How to Bind WPF combo box with xml file

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 data on your screen or you will see a combo with empty values .

Advertisements

11 thoughts on “How to Bind WPF combo box with xml file

  1. Thanks a lot,

    but how to bind a XML-File like This.

    I get no error, but just see the first MAND -> ERP

    THanks
    Regards
    Rene

  2. Hi,

    my XML-File looks like This:

    Mandanten

    ERP
    linnup
    6550
    eks
    sy

    Mandanten

    DEMO
    linnup
    6551
    demo
    sy

    My XMLDataProvider:

    My Problem is, how must the Combobox look like?

    Example does not work:

    Thanks for helping me!
    Regards
    Rene

  3. This was the simplest (and best) explanation I’ve seen yet on how to bind an xml file to a combobox. You did not try to make it something complicated– you stuck to the basics and made it understandable. Thank you!

  4. Thanks for this good tutorial!!
    I have a Combobox with image and text on same line thanks to StackPanel:

    Following your example, how would you do bind both the text AND image link to an XML file:

    Hope that I’m clear..Thanks for your help!!!

  5. Thanks for this good tutorial!!
    I have a Combobox with image and text on same line thanks to StackPanel:
    ComboBoxItem
    StackPanel Orientation=”Vertical”
    Image Source=”image1.png” /Image
    TextBlock Text=”text1″/TextBlock
    /StackPanel
    /ComboBoxItem

    Following your example, how would you do bind both the text AND image link to an XML file:
    ?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

    Hope that I’m clear..Thanks for your help!!!

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