This happens when trying to modify an ObservableCollection<T>
that is bound to a ListBox, for example. This is how you deal with that:
ObservableCollection<Employee> itemsToRemove = new ObservableCollection<Employee>();
foreach (Employee item in lsbxNames.SelectedItems)
{
itemsToRemove.Add(item);
}
foreach (Employee item in itemsToRemove)
{
((ObservableCollection<Employee>)lsbxNames.ItemsSource).Remove(item);
}
- Create a new
ObservableCollection<T>
called itemsToRemove
, with the same T
as your collection you are trying to modify.
- Iterate through your nodes of SelectedItems in your ListBox. Add them to
itemsToRemove
.
- Iterate through
itemsToRemove
. Cast the ListBox ItemsSource to an ObservableCollection<T>
and remove the matches in itemsToRemove
from it.
Reference: http://docs.telerik.com/devtools/wpf/controls/radgridview/managing-data/deleting-entry
So this would mean you should be able to do this:
ObservableCollection<cListEntry> itemsToRemove = new ObservableCollection<cListEntry>();
foreach (cListEntry item in MyList.SelectedItems)
{
itemsToRemove.Add(item);
}
foreach (cListEntry item in itemsToRemove)
{
((ObservableCollection<cListEntry>)MyList.ItemsSource).Remove(item);
}
I'm not sure what _myList
is, but you don't need to modify it. Just go directly to the ListBox.