簡體   English   中英

EntityFramework EntityCollection觀察CollectionChanged

[英]EntityFramework EntityCollection Observing 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更改?

您是否嘗試過處理AssociationChanged在對相關端進行更改時發生。 (繼承自RelatedEnd。)

它提供參數以顯示是否添加或刪除了元素,還公開了該元素。

盡管它可以在@Aron指出的簡單用例中工作,但是我無法在我的實際應用程序中使其正常工作。

事實證明,對於原因,我不知道-內IBindingList一個的EntityCollection不知何故,某處,可以得到改變。 之所以沒有調用我的觀察者,是因為他們正在尋找舊的IBindingList上的更改,而該更改甚至不再由EntityCollection使用。

這是使它對我有用的技巧:

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

您如何映射事件? 粘貼您的代碼並像下面這樣映射事件對我而言有效。

static void Main(string[] args)
    {
        EntityCollection<string> col = new EntityCollection<string>();
        EntityCollectionObserver<string> colObserver = new EntityCollectionObserver<string>(col);

        colObserver.CollectionChanged += colObserver_CollectionChanged;

        col.Add("foo");
    }

    static void colObserver_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Console.WriteLine("Entity Collection Changed");
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM