List 在Windows phone 7中删除多选列表项

List 在Windows phone 7中删除多选列表项,list,windows-phone-7,List,Windows Phone 7,如何删除多选列表中的项目 我的代码工作不正常 for (int i = MyListBox.Items.Count - 1; i >= 0; i--) //for (int i = -1; i <= MyListBox.Items.Count; i++) { if (MyListBox.IsSelectionEnabled == true)

如何删除多选列表中的项目

我的代码工作不正常

                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);
                    }
                }
for(int i=MyListBox.Items.Count-1;i>=0;i--)

//对于(int i=-1;i您无法删除项目,因为您在尝试使用集合时正在更改集合。您需要收集要删除的项目,然后按如下方式删除它们:

            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;
ICollection selectedItems=新列表(MyListBox.selectedItems.Count);
foreach(MyListBox中的变量项。选择编辑项)
{
项目myItem=项目作为项目;
如果(myItem==null)继续;
选择编辑项。添加(我的项目);
}
foreach(selectedItems中的变量项)
{
MyObservable.移除(项目);
}
MyListBox.IsSelectionEnabled=false;