tags:

views:

24

answers:

1

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!

+1  A: 

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
}
Marc Gravell