如何删除多选列表中的项目
我的代码工作不正确
for (int i = MyListBox.Items.Count - 1; i >= 0; i--)
//for (int i = -1; i <= MyListBox.Items.Count; i++)
{
if (MyListBox.IsSelectionEnabled == true)
{
MyObservable.RemoveAt(i);
}
}
MyListBox:multiSelectList
MyObservable:ObservableCollection&lt;&gt;
答案 0 :(得分:0)
您无法删除这些项目,因为您在尝试使用该集合时正在更改该集合。您需要收集要删除的项目,然后将其删除:
ICollection<Item> selectedItems = new List<Item>(MyListBox.SelectedItems.Count);
foreach (var item in MyListBox.SelectedItems)
{
Item myItem = item as Item;
if (myItem == null) continue;
selectedItems.Add(myitem);
}
foreach (var item in selectedItems)
{
MyObservable.Remove(item);
}
MyListBox.IsSelectionEnabled = false;