Binding to XML
The purpose of this tutorial is to show you how to bind RadComboBox to data defined in a XML file. The operations you need to perform are to convert the XML to a collection of business objects and then bind that collection using the ItemsSource property of the ComboBox.
The final result

-
Define the data source as XML content.
Defining the XML data source
XML<?xml version="1.0" encoding="utf-8" ?> <Items> <XmlNodeItem Team="Barcelona" Country="Spain"/> <XmlNodeItem Team="Juventus" Country="Italy"/> <XmlNodeItem Team="Inter" Country="Italy"/> <XmlNodeItem Team="Ac Milan" Country="Italy"/> <XmlNodeItem Team="Real M" Country="Spain"/> <XmlNodeItem Team="Arsenal" Country="England"/> <XmlNodeItem Team="Manchester U" Country="England"/> <XmlNodeItem Team="Bayern" Country="Germany"/> <XmlNodeItem Team="Porto" Country="Portugal"/> <XmlNodeItem Team="Liverpool" Country="England"/> <XmlNodeItem Team="Ajax" Country="Holland"/> <XmlNodeItem Team="Olimpic M" Country="France"/> </Items> -
Add a new
RadComboBoxdeclaration in your XAML.XAML<telerik:RadComboBox x:Name="radComboBox" Width="200"/> -
Create a new class named
XmlNodeItem. This class is pretty simple and it represents a separate node from the XML document. Its properties areTeamandCountry. Both of the properties are of typestring.C#public class XmlNodeItem { [XmlAttribute( AttributeName = "Team" )] public string Team { get; set; } [XmlAttribute( AttributeName = "Country" )] public string Country { get; set; } } -
Create a new class named
XmlNodeItemList, which derives fromObservableCollectionofXmlNodeItem. This is a collection that will be created from the XML file.C#[XmlRoot( ElementName = "Items" )] public class XmlNodeItemList : ObservableCollection<XmlNodeItem> { public void AddRange( IEnumerable<XmlNodeItem> range ) { foreach ( XmlNodeItem node in range ) { this.Add( node ); } } } -
Create a new class named
RadComboBoxXmlDataSource, which derives fromXmlNodeItemList. The class takes a path to the XML file and deserializes the data in the private methodRetrieveData().C#public class RadComboBoxXmlDataSource : XmlNodeItemList { private string source; public string Source { get { return this.source; } set { this.source = value; AddRange(this.RetrieveData(Application.GetResourceStream(new Uri(value, UriKind.Relative)).Stream)); } } private XmlNodeItemList RetrieveData( Stream xmlStream ) { XmlSerializer serializer = new XmlSerializer( typeof( XmlNodeItemList ) ); StreamReader reader = new StreamReader( xmlStream ); XmlNodeItemList list = ( XmlNodeItemList )serializer.Deserialize( reader ); return list; } } -
The next step is to declare the
RadComboBoxXmlDataSourceas a resource in your application.XAML<UserControl.Resources> <example:RadComboBoxDataSource x:Key="DataSource" Source="RadComboBoxBindingToXml.xml"/> </UserControl.Resources> -
Update the
RadComboBoxdeclaration by setting theItemsSourceproperty.XAML<telerik:RadComboBox x:Name="radComboBox" Width="200" ItemsSource="{Binding Source={StaticResource DataSource}}"/> -
Create a custom
DateTemplateand set it to theRadComboBox'sItemTemplateproperty.XAML<DataTemplate x:Key="ComboBoxCustomTemplate"> <StackPanel Margin="5" Orientation="Horizontal"> <TextBlock Text="{Binding Team}" Foreground="Blue"/> <TextBlock Text=" ("/> <TextBlock Text="{Binding Country}"/> <TextBlock Text=")"/> </StackPanel> </DataTemplate> -
Update the ComboBox declaration by setting the
ItemTemplateproperty.XAML<telerik:RadComboBox x:Name="radComboBox" Width="200" ItemsSource="{Binding Source={StaticResource DataSource}}" ItemTemplate="{StaticResource ComboBoxCustomTemplate}"/>
The final result
