如何正确绑定xml到WPF DataGrid? [英] How to bind xml to the WPF DataGrid correctly?

查看:240
本文介绍了如何正确绑定xml到WPF DataGrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找并尝试过各种解决方案,但到目前为止,还没有人解决我的问题。我使用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;

Xaml:

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆