当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - how do i get the index of a listviewitem in wpf

问题描述:

ListViewItem lvi = FindParent<ListViewItem>(sender as WebControl);

ListView lv = FindParent<ListView>(lvi);

int index = lv.items.IndexOf(lvi);

FindParent() goes through the visual tree

I want to get the index of the listviewitem within the listview. Unfortunately, the binding is not allowing me to convert from listviewitem to the bound class. It seems like there should be a simple solution but I cannot find it.

I do not want to select the item, just find its index. Selecting and deselecting is also not good enough. Currently the item is never found, and index is -1. Thank you for any help.

网友答案:

lol I figured it out. Funny how I find the answer 5 minutes after giving up and posting to the internet. Here is it if anyone else is having this same problem.

int index = lv.ItemContainerGenerator.IndexFromContainer(lvi);
分享给朋友:
您可能感兴趣的文章:
随机阅读: