ObservableCollection 优于 ObjectSet [英] ObservableCollection better than ObjectSet
问题描述
为什么(在 WPF、C#、实体框架中)将 ListBox
绑定到基于 ObjectSet
创建的 ObservableCollection
(来自 EntityFramework)而不是直接绑定到ObjectSet
?
Why is it better (in WPF, C#, Entity Framework) to bind ListBox
to an ObservableCollection
created upon the ObjectSet
(from Entity Framework) rather than binding to ObjectSet
directly?
还有一个问题:当我将 ListBox
绑定到 ObservableCollection
时,对集合的任何添加都会更新 ListBox
.伟大的.但是 ObservableCollection
是在 ObjectContext
(在实体框架中)上创建的,并且向集合中添加新项目不会将项目添加到上下文中......如何解决这个问题????
One more question:
When I bind ListBox
to ObservableCollection
, any additions to the collection updates ListBox
. Great. But ObservableCollection
was created upon ObjectContext
(in Entity Framework) and adding a new item to the collection doesn't add the item to the context... how to solve this????
推荐答案
ObservableCollection 实现了 INotifyPropertyChanged
以及 INotifyCollectionChanged
,WPF 使用这两种方法将元素重新绑定到 UI.因此,您可以向 ObservableCollection 添加一个项目,并且 UI 会立即更新,而无需您进行代码交互.ObjectSet
两者都没有实现,因此没有获得此功能.
ObservableCollection implements INotifyPropertyChanged
as well as INotifyCollectionChanged
, both of which WPF uses to rebind elements to the UI. Thus, you could add an item to the ObservableCollection and immediately the UI would update with no code interaction from you. ObjectSet
implements neither, and so doesnt get this functionality.
这篇关于ObservableCollection 优于 ObjectSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!