WPF Datagrid与Xml绑定 [英] WPF Datagrid Bind with Xml
问题描述
大家好,
我必须将一个xml绑定到WPF中的datagrid。
示例输入Xml
< 项目 >
< 项目 标记 = Tag1 value = Value1 / >
< span class =code-keyword>< 项目 tag = Tag2 value = Value2 / >
< 项目 标记 = Tag3 value = Value3 / < span class =code-keyword>>
< Items / >
我必须在设计时将xml绑定到datagrid时间使得xpath条件应该是这样的。
< DataGrid 名称 = dgNoMethod ItemsSource < span class =code-keyword> = {Binding} AutoGenerateColumns = False 宽度 = 自动 高度 = 自动 >
< DataGrid.Columns >
< DataGridTextColumn 标题 = 扩展单位 < span class =code-attribute> Binding = {Binding XPath = Attribute [value] .Value} / >
< < span class =code-leadattribute> DataGridTextColumn 标题 = Header1 绑定 = {Binding XPath = Attribute [value] .Value} / >
< DataGridTextColumn 标题 = Header2 Binding = {Binding XPath = Attribute [value] .Value} / >
< DataGridTextColumn 标题 = Header3 Binding = {Binding XPath = Attribute [value] .Value} / >
< / DataGrid.Columns >
< / DataGrid >
场景:
我想为网格的每一列写一个Xpath查询,这样如果值Header1 Column的标签是'Tag1'然后要绑定到该列的值应该是Value1
所以我想要一个像这样的XPath查询。
< datagridtextcolumn header =Extending Unitbinding ={Bi nding XPath = Items / Item [@ tag ='Tag1']}>
如果Xpath查询具有'Tag1',那么要绑定到Binding属性的值应为Value1 br />
与其他栏类似
要绑定的值Value2和XPath查询应该有tag ='Tag2'
要绑定的值Value3和XPath查询应该有tag ='Tag3'
希望这样可以清除场景。
谢谢,小吃
快乐编码
您可以从这篇简短的文章开始,其中演示了XPath的使用: http ://www.c-sharpcorner.com/UploadFile/mamta_m/binding-xml-to-a-wpf-datagrid [ ^ ]。
< DD> -SA
< blockquote>这是另一个例子 - 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[^].
这篇关于WPF Datagrid与Xml绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!