Binding not updating for DataGrid in Entity Framework 4
I am using Entity Framework 4/WPF/C#. The new ObservableCollection<Entity>(context.EntitySet)
is bound to the WPF DataGrid in XAML. This works fine. But when I programatically create a new Entity and add it to the con开发者_如何学Gotext, DataGrid remains unchanged. What should I do, to get DataGrid to update?
You are not seeing the updates as the ObservableCollection<T>
is obtaining those values from the entity set once, during construction. All additional changes to the entity set are not "observed".
Only calls to ObservableCollection<T>.Add
(et al) generate the CollectionChanged
events.
As sixlettervariables said, the constructor you are using takes your context.EntitySet values and adds them all to the new ObservableCollection. Adding do your context.EntitySet does not add to the duplicated list of items.
Can you show how your EntitySet is defined? Would it be possible to make it an ObservableCollection and bind directly to it?
I didn't see silverlight mentioned so i'm guessing you are not using RIA services. How are you adding the object to the context? .AddTo{EntitySetName]() or .{EntitySetNme}.Add()
If you are binding directly to the data context, I believe the latter option is your best bet. Personally I would shy away from this, as your presentation layer is talking directly to your data layer. You should establish a go between (business layer) that handles adding entities to the UI bound collection and persistence separatly.
well its all right ;) how should the datagrid know that you add an item to your context? the datagrid would just be informed if you add an item to your observable collection.
so if you add the item do your context, just call OnPropertyChanged("YourEntityCollectionPropertyHere") for your collection property.
精彩评论