[英]How do I get Index from listboxitem by its content?
Is there a way to get a Listbox Item Index by its content? 有没有一种方法可以通过其内容获取列表框项目索引? Something like:
就像是:
id = listbox.Items.Contains("text");
I know that this way I'll get a bool result, but I don't know how to get the item Index. 我知道以这种方式可以获得布尔结果,但是我不知道如何获取项目索引。 If I could get the Index I could be able to remove items with
如果我可以获取索引,则可以删除
listbox.Items.RemoveAt(id);
In WPF 在WPF中
ListBox listBox = new ListBox();
int index = listBox.Items.IndexOf(item);
Look for the item's index, and then you can remove it with the given method: 查找项目的索引,然后可以使用给定的方法将其删除:
int index = myListBox.Items.IndexOf(itemToSearch);
/*if there is no coincidence, it returns -1, so you must check for that return, else RemoveAt(-1) would give you a runtime error.*/
if (index >=0)
{
myListBox.Items.RemoveAt(index);
}
Assuming the item is not selected but for some reason you just want to find it in the list: 假设未选中该项目,但由于某种原因,您只想在列表中找到它:
The listbox.items is a listbox object collection: So: listbox.items是一个列表框对象集合:因此:
id=listbox.items.IndexOf("text");
will do the trick. 会成功的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.