What is the purpose of an ObservableCollection to create a PropertyChange in "Item []"?

What is the purpose of the ObservableCollection that creates the PropertyChange property from "Item []"?

Is this something I should do if I have a class that implements INotifyCollectionChanged?

Do WPF controls use this β€œItem []” property somehow?

+4
source share
2 answers

ObservableCollection implements both INotifyCollectionChanged and INotifyPropertyChanged .

INotifyPropertyChanged used to indicate that the ObservableCollection property has changed, as has the number of its elements ( "Count" ), or an element accessible through the collection indexer ( "Item[]" ). In addition, ObservableCollection implements INotifyCollectionChanged to indicate which item has changed exactly and how.

See Monorealization of an ObservableCollection to see what exactly an ObservableCollection does. For example, here is the InsertItem 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 like the right way to implement both INotifyCollectionChanged and INotifyPropertyChanged .

+4
source

Yes WPF and Silverlight controls use the PropertyChange event to update interface elements. This allows you to automatically update items such as a ListView or DataGrid in response to their ObservableCollection binding - or another collection that implements INotifyCollectionChanged -.

Change As for the implementation, you usually don't need to implement your own collection, so you don't need to worry about INotifyCollectionChanged. For your classes to be used in the ObservableCollection, you need to implement INotifyPropertyChanged. This allows your objects to fire the PropertyChanged event whenever they are updated, which allows your user interface to automatically show the changes.

0
source

All Articles