2-way data binding with Entity Framework and WPF DataGrid

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

DataGrid → BoundTo → ListCollectionView → BoundTo → EntitySet

When I interactively add a row to a DataGrid, EntitySet does not add a new entity. However, updating row cell data actually updates the properties of related objects.

Any idea what I can do 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
wpf entity-framework wpfdatagrid
source share
1 answer

Is there any specific reason you are using ListCollectionView ? How do you create your ListCollectionView ?

Calling CollectionViewSource.GetDefaultView( ObjectQuery<> ) gives a BindingListCollectionView . I just ran some tests and typed IEditableCollectionView.AddNew() and IEditableCollectionView.CommitNew() adds a new object to the entity as expected.

I suggest you just bind your ObjectContext ObjectQuery<> property to the ItemsSource for the DataGrid , and the default collection view will be used, ultimately giving you the behavior you expect.

+1
source share

All Articles