Wednesday, 21 May 2014

Usage of ObservableCollection class and INotifyCollectionChanged interface in WPF Data Binding

Usage of ObservableCollection class and INotifyCollectionChanged interface in WPF Data Binding

The ObservableCollection<T> is one of the most important features of WPF data binding. ObservableCollection is a generic dynamic data collection that provides notifications (using an interface "INotifyCollectionChanged") when items get added, removed, or when the whole collection is refreshed.

Namespace for ObservableCollection: System.Collections.ObjectModel

Syntax for ObservableCollection:

[SerializableAttribute]
public class ObservableCollection<T> : Collection<T>, 
INotifyCollectionChanged, INotifyPropertyChanged
ObservableCollection has a lot of properties, methods, events, explicit interface implementations. Get a complete list on MSDN.

Main feature of the ObservableCollection<T> are the events it raises when the items it contains change. When in your WPF application, you make any changes like remove/add/edit item in the ObservableCollection, the ObservableCollection updates the view controls to which it is binded because it internally implements INotifyCollectionChanged and INotifyPropertyChanged interfaces which are responsible for updating the view. INotifyCollectionChanged interface exposes the CollectionChanged event, an event that should be raised whenever the underlying collection changes.

INotifyCollectionChanged Interface: If you are using ObservableCollection, as I already mentioned, you don't need to implement INotifyCollectionChanged interface because it is already implemented in this class. But, if you want to use List instead of ObservableCollection, you will have to surely implement INotifyCollectionChanged if you want your view to get updated when any change is made to your List.

Implementation of INotifyCollectionChanged

public class ViewModelBase : INotifyCollectionChanged
{
  #region INotifyCollectionChanged
  public event NotifyCollectionChangedEventHandler CollectionChanged;
  private void OnNotifyCollectionChanged(NotifyCollectionChangedEventArgs args)
  {
    if (this.CollectionChanged != null)
    {
 this.CollectionChanged(this, args);
    }
  }
  #endregion INotifyCollectionChanged
}

Similarly,  Implementation of INotifyPropertyChanged

public class ViewModelBase : INotifyPropertyChanged
{
  #region INotifyPropertyChanged
  public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged(string propertyName)
  {
    if (PropertyChanged != null)
 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }
  #endregion INotifyPropertyChanged
}

You can inherit ViewModelBase class where you want to implement INotifyCollectionChanged and INotifyPropertyChanged interfaces.

ObservableCollection Example: 

I found a very good and simple example of ObservableCollection in WPF on CSharpCorner. Although, the author is not using MVVM pattern here for the sake of simplicity, you can convert this example to MVVM also.

No comments:

Post a Comment