开发者

What is the purpose of having implemented INotifyPropertyChanged on ObservableCollection?

ObservableCollection implements both INotifyCollectionChanged and INotifyPropertyChanged.

  • I understand that additions, deletions (+ clear), and replacement of items are notifiable to consumers through the collection's event CollectionChanged, and that updates in the existing items can be monitored using the items' event PropertyChanged if they implement themselves INotifyPropertyChanged.

  • I read from others that you can't register on the collection's event PropertyChanged because it is readonly.

So what is its purpose, what utilization can we do of it?

The comments here and there seem to make the discussion confused by implying that the magic of ObservableCollection is to implement both interfaces, allowing to be notified both for collection and items content changes, while this is not correct (this is ignored by many examples where the collection is bound to a listbox which updates magically after items content is changed, suggesting the collection notifies the listb开发者_运维技巧ox).

Actually it seems the only superiority of the collection is to implement INotifyCollectionChanged. Dealing with items property changes seems not at all easier with ObservableCollection than with another collection: it is possible only if the items implements INotifyPropertyChanged, which they may not do, and if the user manages to hook on this event independently of the collection.

Is this correct?


If you look at the ObservableCollection<T> source code with Reflector, you can see that this event is raised for two properties :

this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");

Note that ObservableCollection<T> implements INotifyPropertyChanged explicitly, so you can access the PropertyChanged event only through a INotifyPropertyChanged variable :

INotifyPropertyChanged inpc = myObservableCollection;
inpc.PropertyChanged += myEventHandler;


WPFs binding mechanism can use INotifyPropertyChanged (INpc) out of the box.

INpc as the name suggests allows WPF to detect changes to object properties that may or may not be part of a collection.

ObservableCollection (OC) implements INotifyCollectionChanged (InCC) where as you say the collection itself notifies WPF ( and anyone else equipped to handle the updates) of updates to its items collection (additions deletions etc). If the OC contains objects that do not themselves implement INpc then WPF has no way of knowing how each items properties have changed.

Update

In answering the following question "can we rely on the collection INpc event instead of registering on each new item for being notified ?" the answer is no. If each item does not implement Inpc on its properties then WPF has no way of knowing what values have changed on a per item basis.

WPF will still know from the OC when the items have been added to or deleted in part. The Items property uses the INpc to notify of updates just like any class implementing INpc on its properties. InCC is implemented to track collection changes not values on each item within items.


Just a guess: so one can be notified of changes to the collection's Count property?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜