的EntityFramework EntityCollection观测CollectionChanged [英] EntityFramework EntityCollection Observing CollectionChanged

查看:165
本文介绍了的EntityFramework EntityCollection观测CollectionChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用的EntityFramework数据库的应用程序。我想以某种方式被通知在我的ViewModel变为一个 EntityCollection 的。它不直接支持 INotifyCollectionChanged (为什么?),我还没有找到另一种解决方案是成功的。

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.

这是我最新的尝试,这是因为的ListChanged 事件似乎并不被加注不起作用:

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));
    }
}



没有人有任何想法如何,我可以观察更改一个 EntityCollection

推荐答案

虽然它在简单的用例由@Aron指出的工作,我不能让它在我的实际应用正常工作。

Whilst it worked in the simple use case noted by @Aron, I couldn't get it to work properly in my actual application.

由于事实证明,对于原因,我不知道 - 内 IBindingList的 EntityCollection 不知何故,某处,可以换衣服。我观察员不会被调用的原因是因为他们正在寻找在一个旧的变化 IBindingList的,这不是连正在使用的 EntityCollection 更多

As it turns out, and for reasons I'm not sure - the inner IBindingList of an EntityCollection somehow, somewhere, can get changed. The reason my observers weren't being called is because they were looking for changes on an old IBindingList that wasn't even being used by the EntityCollection any more.

下面是得到它的工作对我来说是黑客:

Here is the hack that got it working for me:

public class EntityCollectionObserver<T> : ObservableCollection<T> where T : class
{
    private static List<Tuple<IBindingList, EntityCollection<T>, EntityCollectionObserver<T>>> InnerLists 
        = new List<Tuple<IBindingList, EntityCollection<T>, EntityCollectionObserver<T>>>();

    public EntityCollectionObserver(EntityCollection<T> entityCollection)
        : base(entityCollection)
    {
        IBindingList l = ((IBindingList)((IListSource)entityCollection).GetList());
        l.ListChanged += new ListChangedEventHandler(OnInnerListChanged);


        foreach (var x in InnerLists.Where(x => x.Item2 == entityCollection && x.Item1 != l))
        {
            x.Item3.ObserveThisListAswell(x.Item1);
        }
        InnerLists.Add(new Tuple<IBindingList, EntityCollection<T>, EntityCollectionObserver<T>>(l, entityCollection, this));
    }

    private void ObserveThisListAswell(IBindingList l)
    {
        l.ListChanged += new ListChangedEventHandler(OnInnerListChanged);
    }

    private void OnInnerListChanged(object sender, ListChangedEventArgs e)
    {
        base.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}

这篇关于的EntityFramework EntityCollection观测CollectionChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆