假设我有简单的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