假設我有簡單的Order類,有計算的財產TotalPrice,它可以綁定到WPF UIINotifyPropertyChanged的和計算的財產
public class Order : INotifyPropertyChanged
{
public decimal ItemPrice
{
get { return this.itemPrice; }
set
{
this.itemPrice = value;
this.RaisePropertyChanged("ItemPrice");
this.RaisePropertyChanged("TotalPrice");
}
}
public int Quantity
{
get { return this.quantity; }
set
{
this.quantity= value;
this.RaisePropertyChanged("Quantity");
this.RaisePropertyChanged("TotalPrice");
}
}
public decimal TotalPrice
{
get { return this.ItemPrice * this.Quantity; }
}
}
它是一個很好的做法,在調用RaisePropertyChanged(「TotalPrice」)影響TotalPrice計算的屬性?什麼是刷新TotalPrice財產的最佳方式? 另一個版本做當然這是改變屬性這樣
public decimal TotalPrice
{
get { return this.ItemPrice * this.Quantity; }
protected set
{
if(value >= 0)
throw ArgumentException("set method can be used for refresh purpose only");
}
}
和呼叫TotalPrice = -1,而不是this.RaisePropertyChanged(「TotalPrice」);在其他物業。請更好的建議解決方案
非常感謝
任何級別的,我不認爲'ItemPrice'和'數量MVVM(在我看來) '應該負責爲'TotalPrice'提高'PropertyChanged'。這會起作用,但如果ItemPrice和Quantity是在另一個類中,那麼你將無法做到這一點,並且必須以另一種方式做到這一點。我在另一個問題中回答了這個問題,答案是相同的,即使這些屬性是在同一個類中,或者它們是在其他類中:http://stackoverflow.com/questions/43653750/raising-propertychanged-for- a-dependent-property-when-a-prerequisite-property-in-another-class – Jogge 2017-04-28 05:42:55