I write the following code to bind the data from a background object to a WinForm UI. I use the INotifyPropertyChanged interface to notify the UI of the property change. But I DIDN'T see any event handler been explicityly assigned to the event PropertyChanged. And I checked my assembly with .NET Reflector and still found no corresponding event-handler? Where is the event handler for PropertyChanged event? Is this yet another compiler trick of Microsoft?
Here is the code of the background object:
public class Calculation :INotifyPropertyChanged
{
private int _quantity, _price, _total;
public Calculation(int quantity, int price)
{
_quantity = quantity;
_price = price;
_total = price * price;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)// I DIDN'T assign an event handler to it, how could
// it NOT be null??
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public int Quantity
{
get { return _quantity; }
set
{
_quantity = value;
//Raise the PropertyChanged event
NotifyPropertyChanged("Quantity");
}
}
public int Price
{
get { return _price; }
set
{
_price = value;
//Raise the PropertyChanged event
NotifyPropertyChanged("Price");
}
}
public int Total
{
get { return _quantity * _price; }
}
}
Many Thansk!