我有这个
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
属性之一被更改时,PropertyChanged
事件将为 Account.Balance
引发:
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
。我该如何以一种易于测试的方式实现这一点?
简而言之:当先决条件属性在另一个类中更改时,如何为一个依赖属性引发PropertyChanged
?
AnyState
是Bank
类中的属性Total
,B.IsRunning
是Account1.Balance
,而C.IsRunning
是Account2.Balance
。@Luishg - Jogge