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?
-
Yes WPF and Silverlight controls use the PropertyChange event to update UI controls. This allows things like ListView's or DataGrid's to automatically update in response to their bound ObservableCollection - or other collection implementing INotifyCollectionChanged - changes.
Edit: As far as implementation goes you generally shouldn't need to implement your own collection so don't need to worrk about INotifyCollectionChanged. For your classes that will be used in the ObservableCollection you need to implement INotifyPropertyChanged. This allows your objects to fire the PropertyChanged event whenever they are updated which will allow your UI control to automatically show the change.
jyoung : I am confused. You say that WPF uses 'PropertyChange of "Item[]"', but I should implement 'CollectionChanged'. -
ObservableCollection
implements bothINotifyCollectionChanged
andINotifyPropertyChanged
.INotifyPropertyChanged
is used to indicate that a property of theObservableCollection
has changed, like the number of its elements ("Count"
) or an element accessible through the collection's indexer ("Item[]"
). Additionally,ObservableCollection
implementsINotifyCollectionChanged
to indicate which element has changed exactly and how.Have a look at the Mono implementation of
ObservableCollection
to see what theObservableCollection
does exactly. For example, here is theInsertItem
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 bothINotifyCollectionChanged
andINotifyPropertyChanged
.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.