This article covers the following topics:
Overview
A binding source object can be treated either as a single object of which the properties contain data or as a data collection of polymorphic objects that are often grouped together (such as the result of a query to a database). For example, a common scenario is to use an ItemsControl such as a ListBox, RadComboBox, or RadTreeView to display a data collection.
The simplest data binding of a RadComboBox control is demonstrated in the following example:
CopyXAML
<telerik:RadComboBox x:Name="radComboBox" ItemsSource="{Binding}" />Instead of RadComboBox you can use RadTreeView,
RadMenu or any other control which inherits from ItemsControl.
Implementing Collections
You can enumerate over any collection that implements the IEnumerable interface. However, to set up dynamic bindings so that insertions or deletions in the collection update the UI automatically, the collection must implement the INotifyCollectionChanged
interface. This interface exposes an event, which should be raised whenever the underlying collection changes.
Tip |
|---|
| Silverlight provides a built-in implementation of a data collection that exposes the INotifyCollectionChanged interface - this is the ObservableCollection(T) class. |
To fully support transferring data values from source objects to targets, each object in your collection must implement the INotifyPropertyChanged interface.
Tip |
|---|
| Consider using ObservableCollection(T) or one of the other existing generic collections, instead of implementing your own collection. |
Implementing Master-Detail Binding Scenario
To know the current item is useful not only for navigation of items in a collection, but also for the master-detail binding scenario. For example, you may have a ListBox which is bound to a collection of objects. Also you have a ContentControl which content is determined by the selection within the ListBox. Or in other words, when a ListBox item is selected, the ContentControl shows the details of the selected item. The following example shows you how to implement such a scenario.
CopyXAML
<ListBox x:Name="listBox" ItemsSource="{Binding Source={StaticResource DataSource}}"/>
<ContentControl x:Name="productDetails" ContentTemplate="{StaticResource ProductDetailsTemplate}" Content="{Binding ElementName=listBox, Path=SelectedItem}"/> Tip |
|---|
| For more information about Data Binding, take a look at the Overview topic. |
See Also