WPF UI にバインドできる TotalPrice 計算プロパティを持つ単純なクラス Order があるとします。
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; }
}
}
TotalPrice の計算に影響するプロパティで RaisePropertyChanged("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");
}
}
this.RaisePropertyChanged("TotalPrice"); の代わりに TotalPrice = -1 を呼び出します。他のプロパティで。より良い解決策を提案してください
どうもありがとう