How Use XML to Bind WPF Datasource
- 1). Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File/New/Project" and click "Visual C#/WPF." A new WPF project is created, and a blank WPF form appears in the main editor window.
- 2). Locate the "Toolbox" panel which should be on the right side of the screen. It contains a list of items that can be added to your graphical user interface. Locate "ListBox" and click-drag it onto the WPF form. When you release the mouse button, the "ListBox" will be placed.
- 3). Locate the window labeled "XML." This contains the source code that determines how your graphical user interface appears.
- 4). Write the following code to set up an XML data source:
<XmlDataProvider x:Key=" boundXMLFile " XPath="/dataSource">
<x:XData> - 5). Enter the following tags into the XML data source:
<cities>
<city name = "Chicago"/>
<city name = "San Francisco"/>
<city name = "Milpitas"/>
<city name = "New York"/>
</cities> - 6). Locate the text tags "<ListBox" and "</ListBox>" (there should be a lot of default text in between these text tags).
- 7). Add the following text between the "ListBox" tags to set the data source to an XML file:
ItemsSource="{Binding Source={StaticResource boundXMLFile},
XPath=city/@name}" - 8). Press the green "Play" button to execute the program. A WPF form appears and displays a "ListBox" with all of the city names you entered into the XML binding.
Source...