Windows Presentation Foundation's ObservableCollection Made Easy | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 18, 2010
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

Advertisement

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.