我怎样才能提高人们对一个ObservableCollection一个CollectionChanged事件,并通过它改变的项目? [英] How can I raise a CollectionChanged event on an ObservableCollection, and pass it the changed items?

查看:192
本文介绍了我怎样才能提高人们对一个ObservableCollection一个CollectionChanged事件,并通过它改变的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从的ObservableCollection 继承,并增加了一些额外的方法,如的AddRange 和<$ C类$ C> RemoveRange

I have a class that inherits from ObservableCollection and adds a few additional methods such as AddRange and RemoveRange

我的基本的方法调用是这样的:

My base method call is this:

public void AddRange(IEnumerable<T> collection)
{
    foreach (var i in collection) Items.Add(i);

    OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}



我这个问题是我要访问 e.NewItems e.OldItems Col​​lectionChanged 事件执行上的操作什么产品集合中,而 NotifyCollectionChangedAction.Reset 操作不会在这些值

My problem with this is that I want to access e.NewItems or e.OldItems in the CollectionChanged event to perform an action on whatever item is in the collection, and the NotifyCollectionChangedAction.Reset action does not pass in these values

void Instances_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems != null) // e.NewItems is always null
    {
        foreach (var item in e.NewItems)
        {
            if (item is EventInstanceModel)
                ((EventInstanceModel)item).ParentEvent = this;
        }
    }
}



所以,我想我可能只是使用 NotifyCollectionChangedAction.Add 而不是重置,但抛出一个范围的行动不支持例外

So I thought I could just use the NotifyCollectionChangedAction.Add instead of Reset, however that throws a Range actions are not supported exception

public void AddRange(IEnumerable<T> collection)
{
    var addedItems = collection.ToList();
    foreach (var i in addedItems) Items.Add(i);

    OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, addedItems));
}



所以我的问题是,我怎么能提出一个CollectionChanged事件,并把它传递新的或旧的项目列表?

So my question is, how can I raise a CollectionChanged event, and pass it the new or old item list?

推荐答案

我一直在寻找到它,显然 Col​​lectionChanged 方法不能与多个项目提高。

I've been looking into it and apparently the CollectionChanged method cannot be raised with multiple items.

所以我可以叫

OnCollectionChanged(new NotifyCollectionChangedEventArgs(
    NotifyCollectionChangedAction.Add, singleItem));



但我不能叫

but I can't call

OnCollectionChanged(new NotifyCollectionChangedEventArgs(
    NotifyCollectionChangedAction.Add, listOfItems));



现在我所做的只是提高添加事件为每个项目增加,但我还是而不高兴这个,因为这意味着我提出了 Col​​lectionChanged 事件在的AddRange 方法,每一个项目,而不是只有一次。

For now what I have done is simply raise the Add event for every item added, but I am still rather unhappy at this since it means I raise the CollectionChanged event for every item in the AddRange method instead of only once.

public void AddRange(IEnumerable<T> collection)
{
    foreach (var i in collection) 
    {
        Items.Add(i);
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(
            NotifyCollectionChangedAction.Add, i));
    }
}

这篇关于我怎样才能提高人们对一个ObservableCollection一个CollectionChanged事件,并通过它改变的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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