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

Posted: March 22, 2011 in Uncategorized

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

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” />

<company name=”Yahoo” />

<company name=”Atticmedia” />

</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}” DisplayMemberPath=”@name”>

</ComboBox>

The interesting part here is the DisplayMemberPath property. You must set it in order to view the binded data on your screen.

Advertisements
Comments
  1. Tarun Juneja says:

    good one

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