实体框架4.0数据绑定与排序不工作 [英] Entity Framework 4.0 Databinding with sorting not working

查看:79
本文介绍了实体框架4.0数据绑定与排序不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些我以为会很简单的事情。我想将生成的Entity Framework EntityCollection绑定到WPF DataGrid。我也希望这个网格可以排序。



我已经尝试过各种各样的事情,包括使用CollectionViewSource。但是,似乎没有任何效果。在EntityCollection周围使用正常的CollectionViewSource可以让我看到:

 'System.Windows.Data.BindingListCollectionView'视图不支持排序。 

Ok ...奇怪我会以为这会奏效接下来的CollectionViewSource,我尝试设置:

  CollectionViewType =ListCollectionView
/ pre>

很好,排序现在可以工作。但是等等,现在我不能使用网格添加或删除实体,可能是因为ListCollectionView不支持实体框架上下文。



所以,我想我需要从数据网格捕获事件,以便从我的上下文手动添加或删除实体。现在我找不到要捕获的事件来检测添加...!



为什么这么难?这应该是Microsoft应该设计的标准演示案例。



任何想法?

解决方案

BindingListCollectionView 不是直接的问题。请参阅' System.Windows.Data.BindingListCollectionView'视图不支持排序在Microsoft Connect上的详细信息,为什么它不支持排序。



另一方面 ListCollectionView 支持使用不同的技术进行排序。



我也尝试过以下代码,并且工作得很好。我基本上从另一篇文章实现了您的XAML在代码中。

  DatabaseContext.ObjectStateManager.ObjectStateManagerChanged + =(o,args)=>的Debug.WriteLine(args.Element.ToString()); 

var collectionViewSource = new CollectionViewSource();
((ISupportInitialize)collectionViewSource).BeginInit();
collectionViewSource.CollectionViewType = typeof(ListCollectionView);
collectionViewSource.Source =((IListSource)DatabaseContext.Survey).GetList();
collectionViewSource.SortDescriptions.Add(new SortDescription {PropertyName =Name});
((ISupportInitialize)collectionViewSource).EndInit();

var editableCollectionView =(IEditableCollectionView)collectionViewSource.View;
var survey = editableCollectionView.AddNew();

//在此之前,发生了ObjectStateManager事件并写入了Debug Output。

editableCollectionView.CommitNew();
DatabaseContext.SaveChanges(); //这个工作太

我的 DatabaseContext.Survey 是一个的ObjectQuery<勘测GT; 。您是否显示一个 ObjectQuery 或Linq-EF查询?前者显然适合我。后者是我看到一个问题的地方。这不应该是有效的。


I want to do something that I thought would be very simple. I want to bind a generated Entity Framework EntityCollection to a WPF DataGrid. I also want this grid to be sortable.

I have tried all kinds of things to make this happen, including using a CollectionViewSource. However, nothing seems to work. Using a normal CollectionViewSource around the EntityCollection gives me:

'System.Windows.Data.BindingListCollectionView' view does not support sorting.

Ok...strange. I would have thought this would work. Next on the CollectionViewSource, I try setting:

 CollectionViewType="ListCollectionView"

Great, sorting now works. But wait, I can't add or remove entities using the grid now, presumably because ListCollectionView doesn't support this with an entity framework context.

So, I guess I need to capture events coming out of the datagrid in order to add or remove entities manually from my context. Now I can't find an event to capture to detect an add...!

Why is this so hard? This should be the standard "demo" case that Microsoft should have designed around.

Any ideas?

解决方案

BindingListCollectionView is not directly the problem. See 'System.Windows.Data.BindingListCollectionView' view does not support sorting on Microsoft Connect for details why it does not support sorting.

On the other hand ListCollectionView supports sorting obviously using a different technique.

I have also tried the following code and it worked beautifully. I have basically implemented your XAML from the other post in code.

 DatabaseContext.ObjectStateManager.ObjectStateManagerChanged += (o, args) => Debug.WriteLine(args.Element.ToString());

 var collectionViewSource = new CollectionViewSource();
 ((ISupportInitialize)collectionViewSource).BeginInit();
 collectionViewSource.CollectionViewType = typeof (ListCollectionView);
 collectionViewSource.Source = ((IListSource) DatabaseContext.Survey).GetList();
 collectionViewSource.SortDescriptions.Add(new SortDescription {PropertyName = "Name"});
 ((ISupportInitialize)collectionViewSource).EndInit();

 var editableCollectionView = (IEditableCollectionView)collectionViewSource.View;
 var survey = editableCollectionView.AddNew();

 // Before this point ObjectStateManager event has occurred and Debug Output is written to.

 editableCollectionView.CommitNew();
 DatabaseContext.SaveChanges(); // THIS WORKS TOO!

My DatabaseContext.Survey is an ObjectQuery<Survey>. Are you showing an ObjectQuery or a Linq-to-EF query? The former obviously works for me. The latter is where I see a problem. That is not supposed to work.

这篇关于实体框架4.0数据绑定与排序不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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