获取ListView中选择的Item的索引 [英] Get the index of Item selected in ListView
问题描述
我已经搜索了大约一个小时,但找不到最佳解决方案。
我正在从VB.NET迁移到C#Forms和C#WPF。
别介意......
因此我将此代码用于C#表单并且它有效,但不适用于C#WPF
I've been searching for about an hour already and couldn't find a best solution. I am migrating from VB.NET to C# Forms and to C# WPF. Never mind that... so I use this code for C# forms and it works, but not in C# WPF
if (ListView1.SelectedItems.Count > 0)
{
for (lcount = 0; lcount <= ListView1.Items.Count - 1; lcount++)
{
if (ListView1.Items[lcount].Selected == true)
{
var2 = lcount;
break;
}
}
}
这是我想要的方式获取在列表框中单击的项目的索引。
我有错误.SELECTED
this is the way I want to get the index of the item clicked in listbox. I have the error in .SELECTED
请帮忙。
推荐答案
您可以从listView获取 SelectedIndex
。无需遍历所有项目,因为根据您的代码,您似乎对任何所选项目的索引感兴趣。
You can get SelectedIndex
from listView. No need to traverse over all items because as per your code you seems to be interested in index of any selected item.
var2 = ListView1.SelectedIndex;
或
如果仅对第一个索引感兴趣:
simply this will work if interested in only first index:
if (lst.SelectedItems.Count > 0)
{
var2 = lst.Items.IndexOf(lst.SelectedItems[0]);
}
这篇关于获取ListView中选择的Item的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!