如何从C#中的Observable集合中删除项目

问题描述:

我正在开发 Windows Phone 应用程序,我想从列表框中删除项目.

I am working on windows phone app and I want to delete item from listbox.

我正在使用以下代码List numbers=new List();

I am using following code List numbers=new List();

 ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);
        int indexPerson = listBox1.SelectedIndex;
        myCollection.RemoveAt(indexPerson);
        var my = (ObservableCollection<string>)listBox1.ItemsSource;
        listBox1.ItemsSource=my;

当我点击删除按钮时,会根据索引删除一个项目,然后当我删除另一个项目时,会显示之前删除的项目,并删除当前删除的项目.

and when I click on delete button one item is deleted based on index and then when I will delete another item the previously deleted item is show and current deleted item is deleted.

如何删除所有项目?

如果你想清除所有项目,你可以使用

If you want to clear all items you can just use

myCollection.ClearItems()

如果您只想删除选定的项目而不是使用

if you want to remove just selected item than use

 myCollection.Remove(Listbox1.Selecteditem)