O'Reilly logo

XAML Developer Reference by Ashish Ghoda, Mamta Dalal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating and Binding to an ObservableCollection

When using an ItemsControl such as a ListBox or TreeView to display a collection of data, you need to bind to the collection and enumerate through its elements.

A collection implementing the IEnumerable interface is eligible for binding to an ItemsControl. The ObservableCollection class enables you to easily bind to collections and also provides notifications whenever you add, remove, or refresh items in the collection. Both WPF and Silverlight support this class. It is defined in the System.Collections.ObjectModel namespace and implements the INotifyCollectionChanged interface.

For example, to bind to a collection of cities, you can use the following XAML markup and code:

XAML Markup:

<Grid x:Name="LayoutRoot"> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required