开发者

Where is the event handler method for the PropertyChaned event?

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 PropertyChange开发者_开发知识库dEventHandler 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 Thanks!


I'm not sure I understand the question, but if you are using data-binding it is the bound control / form that binds to your class - either via the INotifyPropertyChanged interface (as in this case) or as reflection against the *Changed pattern (via PropertyDescriptor). If you really want you could intercept the add/remove parts of the event and look at the stack trace to see who is adding/removing handlers:

private PropertyChangedEventHandler propertyChanged;
public event PropertyChangedEventHandler PropertyChanged {
    add { propertyChanged += value; } // <<======== breakpoint here
    remove { propertyChanged -= value; } // <<===== breakpoint here
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜