Question

If I have a class like the one below:

public class MyClass : INotifyPropertyChanged
{
    private BindingList<String> myList;

    public BindingList<String> MyList
    {
        get { return myList; }
        set
        {
            if (myList == value) return;
            myList = value;
            OnPropertyChanged("MyList");
        }
    }
}

Do I need to setup the following event handler:

myList.ListChanged += (object sender, ListChangedEventArgs e) => OnPropertyChanged("MyList");

Or is it detected? I know that the BindingList class will look for the INotifyPropertyChanged interface on the templated type it is of so it can determine whether it should raise the ListChanged event, but I am uncertain if it works the other way.

Était-ce utile?

La solution

Your myList field is encapsulated within your class - nothing it does is going to "bubble out" automatically. Things that bind to an instance of your class won't know about changes in myList unless you propagate them out with something like the event handler you wrote. However, things that bind to the MyList property are getting a direct reference to the BindingList object, so they will see notifications raised by myList.

Autres conseils

The INotifyPropertyChange interface is for notify when change the properties inside a class. For notify when a collection items changes you must use INotifyCollectionChanged.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top