EntityFramework EntityCollection 观察 CollectionChanged
我首先在应用程序中使用 EntityFramework 数据库。我希望以某种方式收到有关 ViewModel 中 EntityCollection
更改的通知。它不直接支持 INotifyCollectionChanged
(为什么?)并且我还没有成功找到其他解决方案。
这是我的最新尝试,但它不起作用,因为 ListChanged
事件似乎没有引发:
public class EntityCollectionObserver<T> : ObservableCollection<T>, INotifyCollectionChanged where T : class
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public EntityCollectionObserver(EntityCollection<T> entityCollection)
: base(entityCollection)
{
IBindingList l = ((IBindingList)((IListSource)entityCollection).GetList());
l.ListChanged += new ListChangedEventHandler(OnInnerListChanged);
}
private void OnInnerListChanged(object sender, ListChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
有人知道我如何观察 EntityCollection
的更改吗?
担
I'm using EntityFramework database first in an application. I would like somehow to be notified of changes to an EntityCollection
in my ViewModel. It doesn't directly support INotifyCollectionChanged
(why?) and I haven't been successful in finding another solution.
Here's my latest attempt, which doesn't work because the ListChanged
event doesn't appear to get raised:
public class EntityCollectionObserver<T> : ObservableCollection<T>, INotifyCollectionChanged where T : class
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public EntityCollectionObserver(EntityCollection<T> entityCollection)
: base(entityCollection)
{
IBindingList l = ((IBindingList)((IListSource)entityCollection).GetList());
l.ListChanged += new ListChangedEventHandler(OnInnerListChanged);
}
private void OnInnerListChanged(object sender, ListChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
Does anyone have any ideas how I might observe changes to an EntityCollection
?
Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你尝试过处理吗
AssociationChanged 当相关端发生更改时发生。 (继承自RelatedEnd。)
它提供显示元素是否已添加或删除的参数,并且还公开该元素。
have you tried handling
AssociationChanged Occurs when a change is made to a related end. (Inherited from RelatedEnd.)
It gives arguments showing whether an element was added or deleted and also exposes the element.
虽然它在@Aron 指出的简单用例中工作,但我无法让它在我的实际应用程序中正常工作。
事实证明,由于我不确定的原因 -
EntityCollection
的内部IBindingList
会以某种方式在某个地方发生变化。我的观察者没有被调用的原因是他们正在寻找旧的 IBindingList 上的更改,而该旧的 IBindingList 甚至不再被 EntityCollection 所使用。这是让它为我工作的技巧:
Whilst it worked in the simple use case noted by @Aron, I couldn't get it to work properly in my actual application.
As it turns out, and for reasons I'm not sure - the inner
IBindingList
of anEntityCollection
somehow, somewhere, can get changed. The reason my observers weren't being called is because they were looking for changes on an oldIBindingList
that wasn't even being used by theEntityCollection
any more.Here is the hack that got it working for me:
您如何绘制该事件的地图?粘贴您的代码并映射事件如下对我有用。
How are you mapping the event? Pasting your code and mapping the event like follows works for me.