如何从C#中的Observable集合中删除项目 [英] How to Delete Item from Observable collection in C#

查看:29
本文介绍了如何从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)

这篇关于如何从C#中的Observable集合中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆