【发布时间】:2011-03-31 15:55:20
【问题描述】:
我首先在应用程序中使用 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 的变化吗?
丹
【问题讨论】:
标签: c# entity-framework inotifycollectionchanged entitycollection