26

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
  • 122,023
  • 59
  • 287
  • 465

2 Answers2

19

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));
    }
}
Rachel
  • 122,023
  • 59
  • 287
  • 465
  • 1
    You might be interested in this approach: [ListCollectionView/CollectionView doesn't support NotifyCollectionChanged with multiple items](http://geekswithblogs.net/NewThingsILearned/archive/2008/01/16/listcollectionviewcollectionview-doesnt-support-notifycollectionchanged-with-multiple-items.aspx) – Rick Sladkey Sep 16 '11 at 20:51
  • @Rick I checked into something like that, but it doesn't pass the `e.NewItems` / `e.OldItems` into the `CollectionChanged` event – Rachel Sep 16 '11 at 21:36
  • 1
    Why not just put the event call outside the foreach loop right after it, then it would only be called once. – Wobbles May 02 '15 at 15:17
  • 1
    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. – Rachel May 02 '15 at 15:20
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

SuperOli
  • 1,734
  • 1
  • 11
  • 23
  • 1
    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 Sep 16 '11 at 19:51