2
我編寫下面的代碼將數據從背景對象綁定到WinForm UI。我使用INotifyPropertyChanged接口來通知UI屬性更改。但我沒有看到任何事件處理程序明確分配給事件PropertyChanged。我用.NET Reflector檢查了我的程序集,但仍未找到相應的事件處理程序? PropertyChanged事件的事件處理程序在哪裏?這是微軟的又一個編譯器技巧嗎?PropertyChaned事件的事件處理函數方法在哪裏?
這裏是背景對象的代碼:
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; }
}
}
非常感謝!