简体   繁体   中英

How can I raise a CollectionChanged event on an ObservableCollection, and pass it the changed items?

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

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

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

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

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

So I can call

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

but I can't call

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

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

This works fine for me "stand-alone". Meaning I'm not using an ObservableCollection for data binding. So it's not an ObservableCollection issue but rather a ListCollectionView limitation.

Please read the following article, it's a very interesting read:

Nathan Nesbit's Blog

Your post says: 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 .

We're not "stuck" with that behavior, however, because (as many things OOP) if you don't like how something works, you can inherit it to make a custom event that behaves the way you want it to. Example : This custom class inherits from NotifyCollectionChangedEventArgs and I've changed its behavior so that NotifyCollectionChangedAction.Reset does pass in the changed values. (In this case I made a new list named RemovedItems to avoid confusion but one is free to modify existing collections like NewItems or OldItems as well.) I use it in production code and it works well for me.

public class NotifyCollectionResetEventArgs : NotifyCollectionChangedEventArgs
{
    public NotifyCollectionResetEventArgs(List<object> removedItems) 
        : base(action: NotifyCollectionChangedAction.Reset)
    {
        RemovedItems = removedItems.ToList();
    }
    public List<object> RemovedItems { get; }
}

The class that inherits ObservableCollection overrides the Clear method:

public class ObservableAttributeValueCollection : ObservableCollection<object>
{
    List<object> _removedItems = null;
    public new void Clear()
    {
        _removedItems = Items.ToList<object>(); // 1. Capture the items.
        base.Clear();                           // 2. Clear the items
        _removedItems = null;                   // 3. Clean up
    }
    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if(e.Action == NotifyCollectionChangedAction.Reset)
        {
            base.OnCollectionChanged(new NotifyCollectionResetEventArgs(_removedItems));
        }
        else
        {
            base.OnCollectionChanged(e);
        }
    }
}

Finally, consuming this event in a handler uses the 'is' pattern to assign the ePlus variable.

void handleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    switch (e.Action)
    {
        case NotifyCollectionChangedAction.Reset:
            if (e is NotifyCollectionResetEventArgs ePlus)
            {
                foreach (var item in ePlus.RemovedItems)
                {
                    // Do Something
                }
            }
            break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM