Windows Presentation Foundation’s ObservableCollection Made Easy

Introduction

ObservableCollection represents a dynamic data collection that provides an observable notification about the addition or removal of items to the collection.

ObservableCollection is not a replacement for the List, BindingList or Collection classes. It is a variant that has an additional feature. WPF offers this useful class through the System.ComponentModel namespace.

Bindings done to ListBox or a ListView would have to be bound to collections that implement the INotifyCollectionChanged interface. And its CollectionChanged event would intimate the UI of the underlying changes in the collection. Taking this one step ahead is the concept of ObservationCollection. WPF provides this useful class that already implements the INotifyCollectionChanged and the INotifyPropertyChanged interfaces. So when you bind the UI Controls such as ListBox, or a ListView to an ObservableCollection, the controls will be updated with the changes done to the ObservableCollection.

As expected it provides the two events – CollectionChanged and the PropertyChanged.

An important excerpt from MSDN talks about the XAML usage of this object.

“ObservableCollection<T> can be used as a XAML object element in Windows Presentation Foundation (WPF), in versions 3.0 and 3.5. However, the usage has substantial limitations.

  • ObservableCollection<t> must be the root element, because the x:TypeArguments attribute that must be used to specify the constrained type of the generic ObservableCollection<t> is only supported on the object element for the root element.
  • You must declare an x:Class attribute (which entails that the build action for this XAML file must be Page or some other build action that compiles the XAML).
  • ObservableCollection<t> is in a namespace and assembly that is not initially mapped to the default XML namespace. You must map a prefix for the namespace and assembly, and then use that prefix on the object element tag for ObservableCollection.

A more straightforward way to use ObservableCollection<t> capabilities from XAML in an application is to declare your own non-generic custom collection class that derives from ObservableCollection<t>, and constrains it to a specific type. Then map the assembly that contains this class, and reference it as an object element in your XAML.”

ObservableCollection – Class Hierarchy

This is the class hierarchy for the ObservableCollection class.

Class hierarchy for the ObservableCollection class
Figure 1

The CollectionChanged and the PropertyChanged events of the ObservableCollection class make it a strong candidate over regular collection objects. The BeginUpdate and the EndUpdate methods pass the NotifyCollectionChangedEventArgs with the changes to the appropriate events.

ObservableCollection – Definition

ObservationCollection is defined as follows:

  public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged

As you can see, it derives from the Collection<t> class and implements the INotifyCollectionChanged and the INotifyPropertyChanged events.

You can bind the content of the ObservableCollection to any panel, by using the ItemsControl and the ItemsPanelTemplate property.

Good sample codes for usage of ObservableCollections can be found at various places on the holy internet. A few worth mentioning include the MSDN page and the article on “SwitchOnTheCode”. You can find the link to both these articles at the end of this article.

Binding ObservableCollection

You can bind an ObservableCollection in code-behind or XAML. Look for the samples below:

  //Create an ObservableCollection
  public ObservableCollection myFirstObservableCollection = new ObservableCollection();

  //Fill it up.
  foreach (var item in enumerableList)
  {
      myFirstObservableCollection.Add(item);
  }

  //Associate it with the control's ItemSource property
   myControl.ItemsSource = myFirstObservableCollection;

Code Behind

  <ControlName ItemSource="{Binding myFirstObservableCollection } />

XAML Code

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read