将xelement绑定到datagrid [英] Binding xelement to datagrid

查看:73
本文介绍了将xelement绑定到datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用XElement绑定,如果这非常愚蠢,请道歉。我有一个XML需要绑定到DataGrid。



Music.xml:

 <  音乐 >  
< span class =code-keyword>< 相册 Title = Chris Sells Live 艺术家 = Chris Sells ReleaseDate = 2/5/2008 / < span class =code-keyword>>
< 相册 标题 = Redmond之路 艺术家 = Luka Abrus ReleaseDate = 4/3/2008 < span class =code-keyword> / >
< 相册 标题 = 最好的Jim Hance 艺术家 = Jim Hance ReleaseDate = 6/2/2008 / >
< /音乐 >





CodeBehind:

 InitializeComponent(); 
XElement MyMusic = XElement.Load( Music.xml);
this .XElementContainer.DataContext = MyMusic.Elements( Album);





上面的代码从Music.Xml文件中获取XElement



XAML:

 <   DataGrid     x:名称  =  XElementContainer    ItemsSource   =  {Binding}  /  >  





OutputWindow.jpg - Google云端硬盘



上面的链接包含s当前执行的输出,它绑定XElement的属性。我需要绑定指定节点的子元素,即Album,它具有Title,Artist和Release Date的子节点。

如图所示,为什么XElement的绑定属性?我怎么摆脱这个?

我期待datagrid中的输出,我不想创建任何静态DataGridTextColumn。是否可以绑定XElement数据并获得如下结果:



标题|艺术家|发布日期

Chris销售直播Chris Sells 2/5/2008

通往雷德蒙德的路Luka Abrus 4/3/2008

最佳吉姆汉斯Jim Hance 6/2/2008



我尝试了什么:



我对XElement绑定很新。所以上面发布的是我试过的。

解决方案

你好Partha



使用你需要的DataSet以下代码在你的代码背后



 InitializeComponent(); 
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml( Music.xml);
this .XElementContainer.DataContext = ds.Tables [ 0 ];


I'm trying XElement binding for the very first time so apologies if this is very silly. I have an XML which I need to bind to DataGrid.

Music.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>



CodeBehind :

InitializeComponent();
XElement MyMusic = XElement.Load("Music.xml"); 
this.XElementContainer.DataContext = MyMusic.Elements("Album");



Above code gets the XElement from Music.Xml file

XAML :

<DataGrid x:Name="XElementContainer" ItemsSource="{Binding}"/>



OutputWindow.jpg - Google Drive

The above link contains the output of the current execution where it is binding the properties of XElement.I need to bind the child element of specified node i.e Album which has child nodes of Title, Artist and Release Date.
As you see the image, why is it Binding Properties of XElement? How do i get rid of this?
I'm expecting the output in datagrid where I dont want to create any static DataGridTextColumn. Is it possible to just bind the XElement data and get a result like this:

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

What I have tried:

I'm very new to XElement binding. so what ever posted above is what I have tried.

解决方案

Hi Partha

use DataSet you need following code in ur code behind

InitializeComponent();
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml("Music.xml"); 
this.XElementContainer.DataContext = ds.Tables[0];


这篇关于将xelement绑定到datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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