How to construct NotifyCollectionChangedEventArgs for Multiple Replace and Remove

Emon Haque 3,176 Reputation points
2020-10-22T12:28:43.26+00:00

I've 3 ICommand Add, Remove and Replace. Add adds 3 items to the Collection:

void add(object o)  
{  
    var index = Items.Count;  
    for (int i = 0; i < 3; i++)  
    {  
        Items.Add(new Item()  
        {  
            Id = ++count,  
            Name = "Item No. " + count  
        });  
    }  
    var newItems = Items.GetRange(index, Items.Count - index);  
    Items.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newItems, index));  

Remove, removes SelectedItems of the ListBox from the Collection

void remove(object o)  
{  
    var oldItems = new List<object>(o as IEnumerable<object>);  
    foreach (var item in oldItems) Items.Remove(item as Item);  
    Items.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItems));  
}  

when it hits the line Items.OnCollectionChanged..., I get System.InvalidOperationException: 'Collection Remove event must specify item position.' Replace, replaces SelectedItems of the ListBox:

void replace(object o)  
{  
    var oldItems = new List<object>(o as IEnumerable<object>);  
    var newItems = new List<Item>();  
    foreach (var item in oldItems)  
    {  
        var old = item as Item;  
        var @new = new Item()  
        {  
            Id = old.Id,  
            Name = "Replaced " + old.Id  
        };  
        Items[Items.IndexOf(old)] = @new;  
        newItems.Add(@new);  
    }  
    Items.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, oldItems));  
}  

and shows this System.InvalidOperationException: 'Added item does not appear at given index '0'.' when it hits Items.OnCollectionChanged...!

34245-test.gif

How to fix those errors I get in Remove and Replace?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,775 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Emon Haque 3,176 Reputation points
    2020-10-25T02:17:25.413+00:00

    All that I actually need for my Collection is:

    public class Collection<T> : List<T>, INotifyCollectionChanged
    {
        //public Collection() : base() { }
        //public Collection(IEnumerable<T> enumerable) : base(enumerable) { }
    
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        public void OnCollectionChanged(NotifyCollectionChangedEventArgs e) => CollectionChanged?.Invoke(this, e);
    }
    

    and it took a long time to figure out, I wouldn't even think about reducing the code if the IndexOf function of IList didn't give me the invalid cast error. For range actions, the only way, probably, is Reset.

    0 comments No comments

  2. Emon Haque 3,176 Reputation points
    2020-10-28T14:40:42.9+00:00

    For Range Actions, I've to implement the ICollectionView and do things manually. In cases where my SelectedItems are incontiguous, I've to raise the event for each item:

    35791-test1.gif

    Interesting thing is: I don't have to specify LiveGrouping or LiveSorting, it does group and sort automatically! For LiveFiltering, I've to have a method and call that when I change property, not bad!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.