When using ObservableCollection <T> do I still need to implement INotifyPropertyChanged on type T?

The MSDN ObservableCollection<T> page for ObservableCollection<T> notes:

β€œThe objects in your collection must meet the requirements described in the Binding Sources Overview . In particular, if you use OneWay or TwoWay (for example, you want your user interface to be updated when the source properties change dynamically), you must implement a suitable mechanism with the modified properties, for example, INotifyPropertyChanged .

Since ObservableCollection<T> already implements INotifyPropertyChanged , why should I use INotifyPropertyChanged on T again?

+8
inotifypropertychanged observablecollection
source share
2 answers

Consider your observable collection as the data source for the table. Each object from the collection occupies one row and is displayed in the table through several columns.

A view (i.e. your table) should know when to modify each cell in response to changes in the properties of objects, but also in response to adding and removing objects to and from the collection.

Your observed collection takes care of working with table rows: it notifies its observers when an object is inserted, deleted, moved, etc. However, he lacks knowledge about what happens with individual objects, so he does not help to cope with the columns of the table.

Your objects are located here: by implementing INotifyPropertyChanged they allow your table to manage data in columns.

+7
source share

INotifyPropertyChanged must be raised by an object whose properties are being modified. ObservableCollection cannot simply detect changes in the objects it contains and pass them on your behalf.

The reason the collection implements INotifyPropertyChanged is not particularly useful. I suspect that it will only raise a change event for the Count property of the collection, which will change as items are added / removed to the collection.

If you are only interested in those items that are added / removed, you may not need to implement this interface in your class. But if your user interface is tied to the properties of an object, you will need to implement it if you want the user interface to respond.

+3
source share

All Articles