Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Bind Combobox with XML Data

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 732
    Comment on it

    Combobox control is like a dropdown control which contains the list of items. By default it takes no value. There is a property itemsSource with which we bind the items.

    To bind combobox with the XML file,we will right click on the project and add an XML file in the project.

     

     

    Then add the items in an XML file which user wants to display. In XML file user can create his own tags and items. There are not predefined tags for every item. the user can create own and use them. As I have created a Company tag in which I am adding a list of companies names that I want to display in combobox.

     

    See the below code for XML file named Bindddlxmlfile:

     

    <?xml version="1.0" encoding="utf-8" ?> 
    <companies>
    
    <company name="Microsoft.com" />
    
    <company name="Yahoo.com" />
    
    <company name="EvonTechnology.com" />
     
    <company name="Ibm.com" />
      
    <company name="Google.com" />
      
    <company name="Accenture.com" />
    
    <company name="Cognizant.com" />
      
    <company name="Verizon.com" />
    
    </companies>

     

    First in App.Xaml file we will add the application resource so that whole application can have the XML file information scope. So we provide XmlDataProvider in which we add key, XPath and Source. In Source, we provide the path of an XML file.

    <Application.Resources>
            <XmlDataProvider x:Key="Companies" XPath="/companies" Source="/bindddlXmlFile.xml"/>
    </Application.Resources>

    Now we will drag and drop a combobox on window.xaml. We can define some property of combobox accordingly as I have set width, height, and name. Here Mode property is used for the type of selection. One time mode means single selection of an item is possible.

    Now to bind the XML file with combobox, we have to use some property. ItemSource is used to bind the XML data. DisplayMemberPath is used to define the tag property with which we can display a particular property data.

     

    Here is the code for MainWindow.Xaml:

     

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
            Title="MainWindow" Height="350" Width="525">
    
       <Grid>
            <ComboBox x:Name="SelectBox" ItemsSource="{Binding Source={StaticResource Companies}, XPath=company, Mode=OneTime}" DisplayMemberPath="@name" Width="150" Height="30">
    
            </ComboBox>
        </Grid>
        
    </Window>

     

    Now Debug the application and see the result:See the screenshot for reference

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: