【发布时间】:2016-10-07 00:26:17
【问题描述】:
我是第一次尝试 XElement 绑定,如果这很愚蠢,请道歉。我有一个需要绑定到 DataGrid 的 XML。
音乐.xml:
<Music>
<Album Title="Chris Sells Live" Artist="Chris Sells" ReleaseDate="2/5/2008" />
<Album Title="The Road to Redmond" Artist="Luka Abrus" ReleaseDate="4/3/2008"/>
<Album Title="The Best of Jim Hance" Artist="Jim Hance" ReleaseDate="6/2/2008"/>
</Music>
代码隐藏:
InitializeComponent();
XElement MyMusic = XElement.Load("Music.xml");
this.XElementContainer.DataContext = MyMusic.Elements("Album");
以上代码从 Music.Xml 文件中获取 XElement
XAML:<DataGrid x:Name="XElementContainer" ItemsSource="{Binding}"/>
我得到的输出 [![它绑定了 XElement 的属性。我需要绑定指定节点的子元素,即具有 Title、Artist 和 Release Date 子节点的专辑]
我希望在我不想创建任何静态 DataGridTextColumn 的数据网格中输出。是否可以只绑定 XElement 数据并得到这样的结果?:
Title |Artist |ReleaseDate
Chris Sells Live Chris Sells 2/5/2008
The Road to Redmond Luka Abrus 4/3/2008
The Best of Jim Hance Jim Hance 6/2/2008
【问题讨论】:
标签: wpf c#-4.0 datagrid xelement