Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

wpf - Entity Framework 4.0 Databinding with sorting not working

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...