Binding to Collection
This tutorial will guide you through the process of binding a RadComboBox
to a collection of business objects. It also demonstrates two ways of customizing the visualization of the bound items:
To bind the ComboBox to a collection of business objects, you should set its ItemsSource
property. If you want the changes to the collection and the properties of the data items to be automatically reflected in the UI, the collection needs to implement the INotifyCollectionChanged
interface (such collection is ObservableCollection<T>) and your business objects have to implement the INotifyPropertyChanged
interface.
The following example shows how to implement the models structure and use it in the ComboBox control.
Creating the data item's model
Creating the main view model
Initializing the view model as a resource
Setting RadComboBox's ItemsSource
Option 1: Set the DisplayMemberPath Property
By default, the content of the items will be set to the value returned by the ToString()
method of the underlying business objects. As this is rarely the desired behavior, RadComboBox
exposes a DisplayMemberPath
property. Its purpose is to specify a property of the source object to serve as the visual representation of the ComboBox item.
The following example demonstrates how to set the DisplayMemberPath
property to point to the Name
property of the Agency
objects.
Set DisplayMemberPath property
RadComboBox displaying agency names
Option 2: Define a Custom ItemTemplate
The DisplayMemberPath
works well if the value you want to display is contained in a single property of the business objects. If you want to visualize more information, you can define a custom DataTemplate
and set it as the RadComboBox
ItemTemplate
property. The process of doing so is demonstrated below.
Define the custom DataTemplate
Setting the RadComboBox's ItemTemplate property
RadComboBox displaying a custom template