Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of ObservableCollection raising a PropertyChange of "Item[]"?

Tags:

What is the purpose of ObservableCollection raising a PropertyChange of "Item[]"?

Is this something I should be doing if I have a class that implements INotifyCollectionChanged?

Do WPF controls use this PropertyChange of "Item[]" somehow?

like image 738
jyoung Avatar asked Apr 07 '09 23:04

jyoung


1 Answers

ObservableCollection implements both INotifyCollectionChanged and INotifyPropertyChanged.

INotifyPropertyChanged is used to indicate that a property of the ObservableCollection has changed, like the number of its elements ("Count") or an element accessible through the collection's indexer ("Item[]"). Additionally, ObservableCollection implements INotifyCollectionChanged to indicate which element has changed exactly and how.

Have a look at the Mono implementation of ObservableCollection to see what the ObservableCollection does exactly. For example, here is the InsertItem method:

protected override void InsertItem (int index, T item)
{
    CheckReentrancy ();

    base.InsertItem (index, item);

    OnCollectionChanged (new NotifyCollectionChangedEventArgs (
        NotifyCollectionChangedAction.Add, item, index));
    OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
    OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
}

If you want to implement your own ObservableCollection-like collection class, it seems the proper way to implement both INotifyCollectionChanged and INotifyPropertyChanged.

like image 103
dtb Avatar answered Oct 12 '22 02:10

dtb