如何将XML绑定到Silverlight中的DataGrid [英] How to bind XML to a DataGrid in Silverlight

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

问题描述

我有一个从服务器接收XML的应用程序。我想将数据绑定到数据网格,如果网格自动生成列,那将是格外的。到目前为止,我已经在我的代码中尝试了这么多。



XAML页面:

 < data:DataGrid x:Name =StatusItemsSource ={Binding}AutoGenerateColumns =True> 
< / data:DataGrid>

页面后面的代码:

  void Status_OpenReadCompleted(object sender,OpenReadCompletedEventArgs e)
{
XElement recordSet = XElement.Load(e.Result);
CamerasStatusTabDataGrid.ItemsSource = recordSet.Elements(Status);
}

服务器的XML:

 < StatusReport> 
<状态描述=服务器上的溢出咖啡Date =2/5/2009/>
<状态描述=复印机中的小鼠日期=4/3/2008/>
<状态描述=帮助用户查找任何键Date =6/2/2008/>
< / StatusReport>

我想要做的是让状态为网格中的描述和日期是列。

解决方案

一个很好的方法是使用Linq到Xml,以下是一个完整的例子:



Page.xaml:

 < UserControl x:Class =SilverlightApplication1.Page
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/ winfx / 2006 / xaml
xmlns:data =clr-namespace:System.Windows.Controls; assembly = System.Windows.Controls.Data
Width =400Height =300
Loaded =Page_Loaded>

< Grid x:Name =LayoutRootBackground =White>
< data:DataGrid x:Name =DataGrid1/>
< / Grid>
< / UserControl>

Page.xaml.cs:

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Controls;
使用System.Xml.Linq;

命名空间SilverlightApplication1
{
public partial class页面:UserControl
{
public page()
{
InitializeComponent() ;
}

private void Page_Loaded(object sender,RoutedEventArgs e)
{
DataGrid1.ItemsSource = GetStatusReport();
}

public List< Status> GetStatusReport()
{
列表<状态> statusReport = new List< Status>();

//使用XElement.Load(e.Result)获取您的Xml;
XElement doc = XElement.Load(@Data / StatusReport.xml);

statusReport =(从doc.Elements()中的el
选择GetStatus(el))ToList();

return statusReport;
}

私人状态GetStatus(XElement el)
{
状态s = new Status();
s.Description = el.Attribute(Description)。
s.Date = DateTime.Parse(el.Attribute(Date)。Value);
return s;
}
}
}

确保添加引用到System.Xml.Linq程序集。这将使用描述和日期作为列,为网格中的每一行生成您要查找的输出。



alt text http://www.freeimagehosting.net/uploads/aa3f9978fc.png


I have an app that receives XML from a server. I want to bind the data to a data grid and it would be grate if the grid auto generated the columns. So far I have tried to this much in my code.

XAML page:

<data:DataGrid x:Name="Status" ItemsSource="{Binding}" AutoGenerateColumns="True">
</data:DataGrid>

Code behind for the page:

void Status_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    XElement recordSet = XElement.Load(e.Result);
    CamerasStatusTabDataGrid.ItemsSource = recordSet.Elements("Status");
}

XML from the server:

<StatusReport>
    <Status Description="Spilled Coffe on Server" Date="2/5/2009" />
    <Status Description="Mice in Copier" Date="4/3/2008" />
    <Status Description="Helped User Find Any Key" Date="6/2/2008" />
</StatusReport>

What I am looking to do is to have the status be a row in the grid with "Description" and "Date" being columns.

解决方案

A good way to do this is using Linq to Xml, the following is a full example:

Page.xaml:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    Width="400" Height="300"
    Loaded="Page_Loaded">

    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid x:Name="DataGrid1" />
    </Grid>
</UserControl>

Page.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            DataGrid1.ItemsSource = GetStatusReport();
        }

        public List<Status> GetStatusReport()
        {
            List<Status> statusReport = new List<Status>();

            // Get your Xml using XElement.Load(e.Result);
            XElement doc = XElement.Load(@"Data/StatusReport.xml");

            statusReport = (from el in doc.Elements()
                            select GetStatus(el)).ToList();

            return statusReport;
        }

        private Status GetStatus(XElement el)
        {
            Status s = new Status();
            s.Description = el.Attribute("Description").Value;
            s.Date = DateTime.Parse(el.Attribute("Date").Value);
            return s;
        }
    }
}

Make sure you add a reference to the System.Xml.Linq assembly. This produces the output you were looking for with a Status for every row in the grid with "Description" and "Date" as columns.

alt text http://www.freeimagehosting.net/uploads/aa3f9978fc.png

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

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