gpt4 book ai didi

c# - EntityFramework EntityCollection 观察 CollectionChanged

转载 作者:太空狗 更新时间:2023-10-29 23:10:47 26 4
gpt4 key购买 nike

我首先在应用程序中使用 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 的变化吗?

最佳答案

你试过处理吗AssociationChanged 在对相关端进行更改时发生。 (继承自 RelatedEnd。)

它提供参数以显示元素是添加还是删除,并公开该元素。

关于c# - EntityFramework EntityCollection 观察 CollectionChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5502698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com