【发布时间】:2017-09-25 00:15:33
【问题描述】:
我有这个Bank 类:
public class Bank : INotifyPropertyChanged
{
public Bank(Account account1, Account account2)
{
Account1 = account1;
Account2 = account2;
}
public Account Account1 { get; }
public Account Account2 { get; }
public int Total => Account1.Balance + Account2.Balance;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Bank 依赖于其他类,并有一个属性Total,它是根据这些其他类的属性计算得出的。每当这些 Account.Balance 属性中的任何一个发生更改时,都会为 Account.Balance 引发 PropertyChanged:
public class Account : INotifyPropertyChanged
{
private int _balance;
public int Balance
{
get { return _balance; }
set
{
_balance = value;
RaisePropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
每当任何必备属性发生更改时,我都想为Total 提高PropertyChanged。我怎样才能以易于测试的方式做到这一点?
TL;DR当另一个类中的先决属性发生更改时,如何为依赖属性引发 PropertyChanged?
【问题讨论】:
标签: c# wpf xaml data-binding inotifypropertychanged