如何将 xml 正确绑定到 WPF DataGrid? [英] How to bind xml to the WPF DataGrid correctly?
问题描述
我已经寻找并尝试了各种解决方案,但到目前为止都没有解决我的问题.我在 Visual Studio 2010/.NET4 中使用 WPF 的内置 DataGrid 来显示存储为 XDocument 的 XML 文档中的数据.
I have looked for and tried various solutions but so far none of them solve my problem. I am using the built-in DataGrid from WPF in Visual Studio 2010/.NET4 to display data from an XML document stored as an XDocument.
我的代码运行良好,并且我已验证 XDocument 存在且正确.但是,DataGrid 不显示任何数据.
My code all runs fine, and I have verified that the XDocument is present and correct. The DataGrid does not display any data, however.
XML 看起来像这样(为了清晰起见进行了简化):
The XML looks like this (simplified for clarity):
<data>
<track>
<id>211</id>
<name>Track Name</name>
<duration>156</duration>
<artist_id>13</artist_id>
<artist_name>Artist Name</artist_name>
<album_id>29</album_id>
<album_name>Album Name</album_name>
</track>
...
</data>
我的 XAML 如下所示:
My XAML looks like this:
<DataGrid x:Name="LibraryView" Grid.Row="1"
DataContext="{Binding Path=TrackList}" ItemsSource="{Binding XPath=/data/track}">
<DataGridTextColumn Header="Title" Binding="{Binding XPath=name}"/>
<DataGridTextColumn Header="Artist" Binding="{Binding XPath=artist_name}"/>
<DataGridTextColumn Header="Album" Binding="{Binding XPath=album_name}"/>
<DataGridTextColumn Header="Length" Binding="{Binding XPath=duration}"/>
</DataGrid>
支持它的 C# 只是将一个新的 XDocument(从 Web 服务下载)分配给 TrackList 属性(它实现了 INotifyPropertyChanged).不对它做进一步的处理.
The C# that backs it up just assigns a new XDocument (downloaded from a web service) to the TrackList property (which implements INotifyPropertyChanged). No further processing is done on it.
我之前曾尝试使用 XLinq 绑定到查询结果,但也没有奏效(同样的问题),所以我想我会尝试使用 XPath 方法来避免编写可能有问题的 Linq 语句,并尝试发现问题.
I have previously tried using XLinq, to bind to a query result, which didn't work either (same problem), so I thought I'd try the XPath approach to avoid writing a potentially buggy Linq statement, and try to find the problem.
我对如何让 DataGrid 正确显示的想法不多了.我显然缺乏对这应该如何工作的理解,因此我将不胜感激提供的任何帮助.
I am running out of ideas for how to get the DataGrid to display correctly. My understanding of how this is supposed to work is clearly lacking, so I would greatly appreciate any help offered.
值得注意的是,我对输入数据格式有一定的灵活性,因为我自己下载原始 XML.我会尝试一些建议,看看我能做些什么.
It is worth noting that I have some flexibility with the input data format, as I am downloading raw XML myself. I will try some of the suggestions and see what I can get to work.
推荐答案
我使用 XLinq 并且工作正常,使用 XElement 而不是 XDocument :
I used XLinq and worked fine, using a XElement instead of a XDocument :
XElement TrackList = XElement.Load("List.xml");
LibraryView.DataContext = TrackList;
XML:
<DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[track]}">
<DataGrid.Columns>
<DataGridTextColumn Header="Artist" Binding="{Binding Path=Element[artist_name].Value}"/>
<DataGridTextColumn Header="Album" Binding="{Binding Path=Element[album_name].Value}"/>
<DataGridTextColumn Header="Length" Binding="{Binding Path=Element[duration].Value}"/>
</DataGrid.Columns>
</DataGrid>
这篇关于如何将 xml 正确绑定到 WPF DataGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!