Binding XML data to a datagrid

Dec 2, 2008 at 2:13 PM
Hi everyone,

I've got a newbie question regarding XML data binding. I'm trying to create a basic datagrid sample under Blend (using the WPF Toolkit). I'd like to bind my data grid to data coming from a XML file.

Here is a sample of my XML file:
<MyCollection>
    <Item Name="Item 1">
        <Name>My first item<Name/>
        <Type>Type A</Type>
    </Item>
    <Item Name="Item 2">
        <Name>My second item<Name/>
        <Type>Type B</Type>
    </Item>
</MyCollection>

I've created a XmlDataProvider to link my data file to my Blend project. Now I'd like to create a grid with two columns, Name and Type, populated with rows read from my XML file. I didn't figure out how to auto-generate grid columns.

I've also tried to define two DataGridTextColumn into my grid but again I didn't figure out how to bind each column to an XML path.

What should I do?

PS: I can change the XML content & node hierarchy if needed.
Dec 3, 2008 at 5:18 AM
Hi,

 Take a look at this example of how to bind to XML:

http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx#details

With you data, an XPath of "//item" in th XMLDataProvider would make sense, then column XPath bindings of "Name" and "Type". If you cannot get this to work - perhaps you could show us your XAML file also?

Regards,
Colin E.
--
http://wpfadventures.wordpress.com/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples


Dec 3, 2008 at 3:55 PM
Thank for your advice Colin.

In fact, I've found all answers to my question in this thread:    http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=39322