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

41,513

Solution 1

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

Solution 2

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

Solution 3

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;
    }
}
Share:
41,513

Related videos on Youtube

Rachel
Author by

Rachel

"The three chief virtues of a programmer are: Laziness, Impatience and Hubris." - Larry Wall Laziness: I'm too lazy to do the same task repeatedly so write scripts to do that task for me. This makes people think I am intelligent. Impatience: I'm too impatient to wait for my code to run so rewrite the code to improve performance. This makes people think I am a good programmer. Hubris: When someone asks if I can do something I just say Yes, then go find out how to do it (Google!). This makes people think I can do anything. Ultimately, it means I can make a career out of being Lazy, Impatient, and Hubristic.

Updated on February 23, 2022

Comments

  • Rachel
    Rachel about 2 years

    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?

  • Rachel
    Rachel over 12 years
    I did see that link, however he also points out at the bottom in the Not Quite Done section that his version of AddRange doesn't work if more than one item gets added due to the limitations on ListCollectionView. You're right that's it's a limitation of ListCollectionView and not ObservableCollection though
  • Rachel
    Rachel over 12 years
    @Rick I checked into something like that, but it doesn't pass the e.NewItems / e.OldItems into the CollectionChanged event
  • Wobbles
    Wobbles about 9 years
    Why not just put the event call outside the foreach loop right after it, then it would only be called once.
  • Rachel
    Rachel about 9 years
    This question was about how to raise a CollectionChanged event with the parameter of every item that had been added. The answer is that we have to raise the event individually per item, since it does not support being raised with a collection of items.