实体集和WPF数据网格 [英] EntitySets and WPF Datagrids

查看:94
本文介绍了实体集和WPF数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我可能缺少一些小东西,但我无法弄清楚.

我有一个具有EntitySet< t>的类.作为其成员之一.我也有一个带DataGrid的WPF页面.我已经创建了我的类的ViewModel,并为我的类中的EntitySet提供了一个公共属性(带有OnPropertyChanged事件等等).

我将DataGrid的ItemsSource绑定到ViewModel的EntitySet属性.
它获取了在EntitySet中已存在数据的类的在DataGrid上显示的Entity set中的所有数据,但是当我创建类的新实例并初始化EnitySet时,DataGrid不会显示任何内容.

我希望能够通过DataGrid将数据添加到\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.我该怎么做,如何将列设置为需要这样的字段的组合框?

在此先感谢

好的,经过一番尝试之后,我注意到如果实体集不包含任何条目(如上述情况),则不会创建列及其名称,但是确实存在空白记录.

所以我的问题应该是,如何将列添加到datagrid?

Hi All,

There is probably something small I''m missing, but I can''t figure it out.

I have a Class which has an EntitySet<t> as one of its members. I also have a WPF Page with a DataGrid. I have create a ViewModel of my class, with a Public Property for the EntitySet within my class (with the OnPropertyChanged events and blah blah).

I have the DataGrid''s ItemsSource bound to the EntitySet property of the ViewModel.

It get all the data in the Entity set displayed on the DataGrid for a class that already has data in the EntitySet, but when I create a new instance of my class and initialise the EnitySet, the DataGrid shows nothing.

I would like to be able to add\remove data to the EntitySet via the DataGrid (so that if I use LINQ2SQL''s DataContext.SubmitChanges(), my database is updated). How would I go about doing this, and how would I be able to set the columns to comboboxes where the field needs to be such?

thanks in advance

Ok, after playing around with it a bit, I noticed that if the entity set does not contain any entries (as is the case above) the columns and their names are not created, but there does exist a blank record.

So my question should be, how can I add the columns to the datagrid?

推荐答案

我意识到,您绑定了控件的ItemsSource属性到一个空集合,然后填充您的集合.物品消失的可能原因是没有通知装订集的更改.

As I realized, you bound the ItemsSource property of your control to an empty collection and, filled your collection afterwards. Possible reason of the disappearance of the items is that the binding isn''t notified about the collection''s changes.

为了使绑定能够收到有关集合更改的通知,必须将ItemsSource属性绑定到实现 ObservableCollection .

In order to let the binding to be notified about the collection''s changes, you have to bind the ItemsSource property to a collection that implements INotifyCollectionChanged. For that purpose you can use ObservableCollection.

您可以将视图模型中的EntitySet属性替换为相应的ObservableCollection属性.

You can replace the EntitySet property in your view-model with a corresponding ObservableCollection property.


好,所以我可以正常工作了...我只需手动在datagrid上创建所需的列,并将其绑定到Entity集中包含的类的适当属性即可.真的很简单
Ok, so I got this working... I just needed to manually create the columns that I want on the datagrid and bind that to the appropriate properties of the class contained in the Entity set. really simple actully


这篇关于实体集和WPF数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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