Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to bind XML Data using XmlDataProvider in WPF?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 822
    Comment on it

    In WPF, we can easily bind WPF controls with xml data using XmlDataProvider. Using XmlDataProvider, we can bind xml data in XAML file itself without writing any code in code behind.


    Here, below is the example of XmlDataProvider.

    For binding xml data using XmlDataProvider ,first we require xml file that contains data.


    XML file:

    <?xml version="1.0"?>
        <Name>Group 1</Name>
        <Name>Group 2</Name>
        <Name>Group 3</Name>
        <Name>Group 4</Name>


    In XAML:


    In XmlDataProvider,

    1. In Source -we have to pass path of xml file. 
    2. In XPath- we have to pass path required xml node 


    <XmlDataProvider x:Key="groupConfigurationXmlDataProvider" Source="XmlFiles/ImagesConfiguration.xml" XPath="/Groups" />


    Here below, I bind ItemsControl using XmlDataProvider.

    In DataContext- Provide XmlDataProvider. In my case it is "groupConfigurationXmlDataProvider".
    In ItemSource- Provide xml node path.
    In Content- Provide the node which you have to display.In my case I provide "Name" .


    Binding Item Control

     <ItemsControl HorizontalAlignment="Left" VerticalAlignment="Bottom" DataContext="{Binding Source={StaticResource groupConfigurationXmlDataProvider}}" ItemsSource="{Binding XPath=Group}">      
                            <StackPanel Orientation="Horizontal"/>
                           <Button Content="{Binding XPath=Name}" Tag="{Binding}" Style="{StaticResource Button_Header}" Click="Button_Click" />


    Hope, this code will help you. Thanks


 0 Comment(s)

Sign In

Sign up using

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: