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

3

27

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?

Suppurate answered 16/9, 2011 at 18:57 Comment(0)
S
20

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));
    }
}
Suppurate answered 16/9, 2011 at 19:22 Comment(3)
@Rick I checked into something like that, but it doesn't pass the e.NewItems / e.OldItems into the CollectionChanged eventSuppurate
Why not just put the event call outside the foreach loop right after it, then it would only be called once.Unselfish
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.Suppurate
T
5

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

Tullusus answered 16/9, 2011 at 19:36 Comment(3)
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 thoughSuppurate
The link is now broken.Derogatory
Link should be good now.Tullusus
D
1

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;
    }
}
Derringdo answered 22/2, 2022 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.