将WPF DataGrid绑定到对象数组 [英] Binding WPF DataGrid to Array of Objects

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

问题描述

我正在尝试将网格绑定到Xml文件中的数据.我使用xsd.exe首先从xml创建一个架构,然后从该架构创建一个类文件.我可以在代码中将数据绑定到网格,但是在设计器中找不到这样做的方法.在winforms中,我将从对象创建一个DataSource,并可以使用它在设计器中配置DataGridView列.但是我找不到在WPF设计器中将DataGrid.ItemSource指向它的方法.

我的xaml,

I''m trying to bind my grid to data from an Xml file. I''ve used xsd.exe to create a first a schema from the xml, and then a class file from the schema. I can bind the data to the grid in code, but can''t find how to do so in the designer. In winforms I would create a DataSource from the object, and could use that to configure the DataGridView columns in the designer; but I can''t find a way to point DataGrid.ItemSource to it in the WPF designer.

My xaml,

<DataGrid AutoGenerateColumns="True" HorizontalAlignment="Stretch" Margin="12,320,12,12" Name="dataGrid1" VerticalAlignment="Stretch"  />





XmlReader reader = XmlReader.Create(filePath);
XmlSerializer serializer = new XmlSerializer(typeof(myXml));
myXml data = new myXml();
data = (myXml)serializer.Deserialize(myXml);

//data.entry is an array of myXmlEntry objects
dataGrid1.ItemsSource = data.entry;

推荐答案

您需要引用资源中的XML,通常可以通过以下方式实现:
You need to reference the XML in the resources, you can do this typically by :
<Window.Resources>
  <XmlDataProvider x:Key="MyXML" d:IsDataSource="True" Source="MyXml.xml" />
</Window.Resources>

然后,使用

ItemsSource="{Binding Source={StaticResource MyXML}}"


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

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