views:

562

answers:

5

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 fuelled by many examples where the collection is bound to a listbox which updates magically after items content is changed, suggesting the collection notifies the listbox).

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?

A: 

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

Groky
+1  A: 

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.

Andrew
+2  A: 

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;
Thomas Levesque
While factually accurate and succinct I am not sure how that answers the "so whats the purpose?" part of the Q
Andrew
The purpose is to notify subscribers that the collection has changed. In that aspect it serves the same purpose as the CollectionChanged event, but INotifyPropertyChanged is more widely supported than INotifyCollectionChanged
Thomas Levesque
like in case something binds to the count of the collection..
dan
A: 

Groky, Andrew, thanks a lot. I forgot there are also properties in ObservableCollection... Does it mean PropertyChanged event in ObservableCollection is usable and is not protected?

Still I'm confused in the case the items implement INpc. In this case, can we rely on the collection INpc event instead of registering on each new item for being notified (said otherwise, does the collection convert any INpc notification from items into a collection notification?

wpf
"does the collection convert any INpc notification from items into a collection notification?" No. If you want to receive notifications from contained items, you must subscribe to their PropertyChanged events separately. (Obviously when data binding WPF will do this for you.)
itowlson
I have updated my answer in response to your question. Stackoverflow does not normally encourage further Qs via original posters adding answers to their own questions but I can understand you doing this as there is a rep limit on comments.
Andrew
A: 

I learnt in 60 mn that:

  • As ObservableCollection.PropertyChanged is an explicit implementation of the interface it must be subscribed to using:

    (myObservableCollection as INotifyPropertyChanged).PropertyChanged += myEventHandler;

  • ObservableCollection.PropertyChanged is useful for other uses than receiving notification for items content updated, like Count changes.

  • Items updates must be monitored using each item's PropertyChanged event separately, ObservableCollection doesn't repeat or sum these notifications.

I appreciate all your answers. Thanks.

wpf