Code
public class Order: BaseViewModel
{
private double _price;
private double _quantity;
public double Price
{
get {return _price; }
set
{
if (_price == value)
return;
_price = value;
RaisePropertyChanged ( "Price");
}
}
public double Quantity
{
get {return _quantity; }
set
{
if (_quantity == value)
return;
_quantity = value;
RaisePropertyChanged ( "Quantity");
}
}
public double TotalPrice {get {return Price * Quantity; }}
}
public class BaseViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged (string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged! = null)
propertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
If Price will be changed in the code, the price changes are automatically displayed in the View, because ViewModel View report of Price Change by calling the event RaisePropertyChanged ( «Price»). Computed TotalPrice did not change in the View, because no one is RaisePropertyChanged ( «TotalPrice»). You can call RaisePropertyChanged ( «TotalPrice») in the same places where called RaisePropertyChanged ( «Price») and RaisePropertyChanged ( «Quantity»), but do not want to spread over a plurality of information on places that TotalPrice depends on Price and Quantity, and I would like to store this information in one place. To this end, a variety of people write dependency management, but let's see what is the minimum code is actually needed for this.