2-Way databinding with Entity Framework and WPF DataGrid

倾然丶 夕夏残阳落幕 提交于 2019-12-23 03:24:24

问题


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

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?

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>

回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/2700792/2-way-databinding-with-entity-framework-and-wpf-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!