我想要列表视图中的特定项目的索引 例如 ListViewItem索引
Apple 0
Ball 1
Cat 2
Dog 3
如果我通过" Cat"作为一个项目,它应该返回Cat As" 3"
的索引答案 0 :(得分:0)
ListView
项目包含在ItemSource
IEnumerable
您可以通过执行以下操作来获取索引:(假设您的上面列表是List< string>)
(myListView.ItemSource as List<String>).IndexOf("Cat");
但是,我强烈建议您在构建WPF应用程序时查看MVVM的工作原理,可以使用Google找到大量信息和教程。
答案 1 :(得分:0)
最后解决方案: int index = MyListView.Items.IndexOf(&#34; Cat&#34;);
答案 2 :(得分:-2)
我认为ListView控件的SelectedInex是您正在寻找的。看看:
我的示例代码:
// Assuming that this is your control
ListView yourControl = new ListView();
yourControl.Items.Add(new ListViewItem() { Content = "Apple" });
yourControl.Items.Add(new ListViewItem() { Content = "Ball" });
yourControl.Items.Add(new ListViewItem() { Content = "Cat" });
yourControl.Items.Add(new ListViewItem() { Content = "Dog" });
// get your index
var newList = yourControl.Items.OfType<ListViewItem>().Select(Item => Item.Content.ToString()).ToList();
int catIndex = newList.IndexOf("Cat"); // <-- your index