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...
!
How to fix those errors I get in Remove and Replace?