2010-02-04 96 views
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; } 
    } 
} 

非常感謝!

回答

1

我不確定我是否理解這個問題,但是如果您使用的是數據綁定,它是綁定到您的課程的綁定控件/表單 - 可以通過接口(如本例中)或反射*Changed模式(通過PropertyDescriptor)。如果你真的你可以截獲該事件的add/remove部分,並期待在堆棧跟蹤,看看誰在添加/刪除處理程序:

private PropertyChangedEventHandler propertyChanged; 
public event PropertyChangedEventHandler PropertyChanged { 
    add { propertyChanged += value; } // <<======== breakpoint here 
    remove { propertyChanged -= value; } // <<===== breakpoint here 
} 
相關問題