簡體   English   中英

使用FloatProperty將ObjectProperty綁定到其他屬性

[英]Bind ObjectProperty with FloatProperty inside to other property

我有一個Article類,里面有一些Properties

public class Article {
    private IntegerProperty id;
    private StringProperty name;
    private StringProperty description;
    private FloatProperty price;

    public Article(int id, String name, String description, float price) {
        this.id = new SimpleIntegerProperty(id);
        this.name = new SimpleStringProperty(name);
        this.description = new SimpleStringProperty(description);
        this.price = new SimpleFloatProperty(price);
    }

    ...
}

我有一個Sale類,它引用了一篇文章。

public class Sale {
    private IntegerProperty id;
    private ObjectProperty<Article> article;
    private FloatProperty discount;
    private IntegerProperty quantity;
    private FloatProperty total;

    public Sale(int id, Article article, float discount, int quantity) {
        this.id = new SimpleIntegerProperty(id);
        this.article = new SimpleObjectProperty<>(article);
        this.discount = new SimpleFloatProperty(discount);
        this.quantity = new SimpleIntegerProperty(quantity);
        this.checked = new SimpleBooleanProperty(false);

        this.total = new SimpleFloatProperty();
        this.total.bind(Bindings.multiply(Bindings.subtract(this.article.get().priceProperty(), Bindings.multiply(this.article.get().priceProperty(), this.discount)), this.quantity));
    }

    public void setArticle(Article article) {
        this.article.set(article);
    }
    ...
}

如何正確綁定整個屬性? 現在如果我做這樣的事情,總不會在tableView上改變。

Sale sale = new Sale(0, article1, 0.1f, 2);
sale.setArticle(article2); 

我想我應該參考articleProperty而不是值,但我不知道如何。

你可以使用Bindings.selectFloat

this.total.bind(Bindings.multiply(Bindings.selectFloat(this.article, "price")
                                          .multiply(this.quantity),
                                  Bindings.subtract(1d, this.discount)));

你可以做:

public Sale(int id, Article article, float discount, int quantity) {
    this.id = new SimpleIntegerProperty(id);
    this.article = new SimpleObjectProperty<>(article);
    this.discount = new SimpleFloatProperty(discount);
    this.quantity = new SimpleIntegerProperty(quantity);
    this.checked = new SimpleBooleanProperty(false);

    this.total = new SimpleFloatProperty();

    FloatProperty articlePriceProperty = new SimpleFloatProperty();
    if (this.article != null) {
        articlePriceProperty.bind(article.priceProperty());
    }
    this.article.addListener((obs, oldArticle, newArticle) -> {
        articlePriceProperty.unbind();
        if (newArticle == null) {
            articlePriceProperty.set(0);
        } else {
            articlePriceProperty.bind(newArticle.priceProperty());
        }
    });
    this.total.bind(Bindings.multiply(Bindings.subtract(articlePriceProperty, Bindings.multiply(articlePriceProperty(), this.discount)), this.quantity));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM