且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF Datagrid与Xml绑定

更新时间:2022-12-27 15:51:37

您可以从这篇简短的文章开始,其中演示了XPath的使用: http ://www.c-sharpcorner.com/UploadFile/mamta_m/binding-xml-to-a-wpf-datagrid [ ^ ]。



-SA

这是另一个例子 - A使用XML数据的DataGrid示例 [ ^ ]。

XPath - 这是另一个例子 - 使用WPF进行数据绑定:绑定到XML [ ^ ]。


Hi All,
I have to bind a xml to datagrid in WPF.

Sample Input 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.

<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"

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


Here is another example - A DataGrid sample using XML data[^].
XPath - here is another example - Data Binding with WPF: Binding to XML[^].