Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have to bind a xml to datagrid in WPF.

Sample Input Xml
XML
<Items>
  <Item tag="Tag1" value="Value1"/>
  <Item tag="Tag2" value="Value2"/>
  <Item tag="Tag3" value="Value3"/>
<Items/>


I have to bind the xml to datagrid at design time such that the xpath condition should be like this.
XML
<DataGrid Name="dgNoMethod" ItemsSource="{Binding}" AutoGenerateColumns="False" Width="Auto" Height="Auto">
<DataGrid.Columns>
 <DataGridTextColumn Header="Extending Unit" Binding="{Binding XPath=Attribute[value].Value}"  />
                <DataGridTextColumn Header="Header1" Binding="{Binding XPath=Attribute[value].Value}" />
                <DataGridTextColumn Header="Header2" Binding="{Binding XPath=Attribute[value].Value}" />
                <DataGridTextColumn Header="Header3" Binding="{Binding XPath=Attribute[value].Value}" />
            </DataGrid.Columns>
</DataGrid>


Scenario:

I want to write a Xpath query for each column of the grid such that if the value of tag for Header1 Column is 'Tag1' then the value to be binded to that column should be Value1

So I want a XPath query somewhat like this.
<datagridtextcolumn header="Extending Unit" binding="{Binding XPath=Items/Item[@tag='Tag1']}">
AT the end the value to be binded to the Binding attribute should be Value1 if the Xpath query has 'Tag1'

Similary for other columns
Value to be binded Value2 and XPath query should have tag='Tag2'
Value to be binded Value3 and XPath query should have tag='Tag3'


Hope this clears the scenario.

Thanks, Tapas
"Happy Coding"
Posted

You can start with this short article where the use of XPath is demonstrated: http://www.c-sharpcorner.com/UploadFile/mamta_m/binding-xml-to-a-wpf-datagrid[^].

—SA
 
Share this answer
 
Comments
Abhinav S 5-Feb-14 12:06pm    
5 of course.
Sergey Alexandrovich Kryukov 5-Feb-14 12:18pm    
Thank you, Abhinav.
—SA
Here is another example - A DataGrid sample using XML data[^].
XPath - here is another example - Data Binding with WPF: Binding to XML[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900