只需創建一個基類,它從INotifyPropertyChanged接口繼承這樣的:
public abstract class BaseViewModel : INotifyPropertyChanged
{
#region members
protected IUnitOfWork UnitOfWork;
#endregion
public BaseViewModel()
{
}
//basic ViewModelBase
internal void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
public event PropertyChangedEventHandler PropertyChanged;
}
那麼您可以在您的具體類使用這樣的:
public class TransactionItemViewModel : BaseViewModel
{
int _Quantity;
public int Quantity
{
get
{
return _Quantity;
}
set
{
if (_Quantity != value)
{
_Quantity = value;
RaisePropertyChanged("Quantity");
RaisePropertyChanged("TotalSalePrice");
}
}
}
public decimal TotalSalePrice
{
get
{
return 100 * Quantity;
}
}
}
您需要確保捕獲每個實例,非常複雜。 – OverMars