实体框架和WPF DataGrid的双向数据绑定 [英] 2-Way databinding with Entity Framework and WPF DataGrid

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

问题描述

我无法使用WPF 4.0 DataGrid的添加功能将Entity Framework实体添加到ObjectContext的EntitySet中。这里是设置:

I'm having trouble adding an Entity Framework entity to a ObjectContext's EntitySet automatically using the WPF 4.0 DataGrid's add functionality. Here's the setup:

DataGrid - > BoundTo - > ListCollectionView - > BoundTo - > EntitySet

DataGrid-->BoundTo-->ListCollectionView-->BoundTo-->EntitySet

当我向DataGrid交互式添加一行时,EntitySet没有添加一个新的实体。更新行的单元格数据实际上会更新绑定的实体的属性。

When I interactively add a row to the DataGrid, the EntitySet does not have a new entity added to it. Updating the row's cell data does in fact update the bound entity's properties, however.

任何想法我可能会做错什么?

Any idea what I could be doing wrong?

这是ListCollectionView的XAML:

Here is the XAML for the ListCollectionView:

<CollectionViewSource x:Key="FieldList"
    Source="{Binding DB.Fields}"
    CollectionViewType="{x:Type data:ListCollectionView}">
    <CollectionViewSource.SortDescriptions>
        <ComponentModel:SortDescription PropertyName="Name" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>


推荐答案

有什么特别的原因你使用的ListCollectionView ?如何创建您的 ListCollectionView

Is there any particular reason why you are using ListCollectionView? How are you creating your ListCollectionView?

调用 CollectionViewSource.GetDefaultView(ObjectQuery& )产生一个 BindingListCollectionView 。我刚刚运行一些测试,并调用 IEditableCollectionView.AddNew() IEditableCollectionView.CommitNew()向实体添加新实体设置为预期。

Calling CollectionViewSource.GetDefaultView( ObjectQuery<> ) yields a BindingListCollectionView. I have just run some tests and calling IEditableCollectionView.AddNew() and IEditableCollectionView.CommitNew() adds new entity to entity set as expected.

我建议你只需绑定你的 ObjectContext ObjectQuery& ; 属性到 DataGrid 的I​​temsSource ,并将最终使用默认集合视图给你你期望的行为。

I suggest you simply bind your ObjectContext's ObjectQuery<> property to ItemsSource of a DataGrid and the default collection view will be used, ultimately giving you the behavior you expect.

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

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