How to Delete Item from Observable collection in C#

别等时光非礼了梦想. 提交于 2019-12-25 21:43:13

问题


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

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.

How can I delete all items?


回答1:


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)


来源:https://stackoverflow.com/questions/30567951/how-to-delete-item-from-observable-collection-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!