What is the purpose of implementing INotifyPropertyChanged in an ObservableCollection?

ObservableCollection implements both INotifyCollectionChanged and INotifyPropertyChanged .

  • I understand that additions, deletions (+ clearly), and replacement of items are notified to consumers through the CollectionChanged collection event, and updates to existing items can be controlled using item events. PropertyChanged if they implement INotifyPropertyChanged themselves.

  • I read from others that you cannot register for an PropertyChanged collection event because it is immutable.

So what is its purpose, what use can we make with it ?

The comments here and there seem to make the discussion confused, implying that the magic of the ObservableCollection is to implement both interfaces, allowing you to receive notifications both about changes to the contents of the collection and that it is incorrect (this is ignored by many examples where the collection is tied to a list that is magically updated after changing the contents of the elements, prompting the collection to notify the list).

In fact, it seems that the only benefit of the collection is the implementation of INotifyCollectionChanged . Working with changes to the properties of elements does not look so simple with an ObservableCollection than with another collection: this is possible only if the elements implement INotifyPropertyChanged , which they may not do, and if the user manages to bind this event independently of the collection.

Is it correct?

+7
wpf inotifypropertychanged observablecollection
source share
3 answers

If you look at the source code of the ObservableCollection<T> using Reflector, you will see that this event was created for two properties:

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

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

 INotifyPropertyChanged inpc = myObservableCollection; inpc.PropertyChanged += myEventHandler; 
+9
source share

The WPF binding engine can use INotifyPropertyChanged (INpc) out of the box.

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

ObservableCollection (OC) implements INotifyCollectionChanged (InCC), where, as you say, the collection itself notifies WPF (and everyone who has the ability to handle updates) of updates to its collection of elements (adding exceptions, etc.). If OC contains objects that themselves do not implement INpc, WPF has no way of knowing how the properties of each element have changed.

Update

Answering the following question: "Can we rely on an INPC collection event instead of registering for every new item to receive a notification?" the answer is no. If each element does not implement Inpc in its properties, WPF has no way of knowing which values ​​have been changed for each position.

WPF will still know from OC when items were partially added or removed. The Items property uses INpc to notify you of updates just like any class that implements INpc in its properties. InCC is implemented to track changes to the collection of non-values ​​for each item in the items.

+3
source share

Just to guess: is it possible to notify about changes in the Count Count property?

+1
source share

All Articles