How to Bind WPF combo box with xml file with image and text

This article demonstrates you how to bind WPF combo box with xml file data or with XmlDataProvider.

Use this post if you want to bind a wpf combo box with a xml file

Use this post if you want to bind and display some images in combo box

Use this post if you want to learn how you can customize the display of combo box

Create a Xml file, in my case it is static one but you can create one from the database programmatically.

<?xml version=”1.0″ encoding=”utf-8″ ?>

<companies>

<company name=”Microsoft” icon=”image1.png”/>

<company name=”Yahoo” icon=”image2.png”/>

<company name=”Atticmedia” icon=”image3.png”/>

</companies>

You can add more attributes as per your requirements.

Now goto you app.xaml and add below code in it

<Application.Resources>

<XmlDataProvider x:Key=”Companies” XPath=”/companies” Source=”/DataSource/CompanyData.xml”/>

</Application.Resources>

Declare a combo box on your page as shown below

<ComboBox x:Name=”SelectBox” ItemsSource=”{Binding Source={StaticResource Companies}, XPath=company, Mode=OneTime}” >

<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation=”Horizontal”>
<TextBlock Text=”{Binding XPath=@name}” Margin=”4,0″/>
 <Image Source=”{Binding XPath=@icon}” />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

Advertisements

One thought on “How to Bind WPF combo box with xml file with image and text

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s