Skip to content

Instantly share code, notes, and snippets.

@emoacht
Created March 23, 2022 04:16
Show Gist options
  • Save emoacht/c48d0b03cf28056756167ba433b387a6 to your computer and use it in GitHub Desktop.
Save emoacht/c48d0b03cf28056756167ba433b387a6 to your computer and use it in GitHub Desktop.
Get index number of ListViewItem in ListView.
public class ListViewHelper
{
public static int GetIndex(object item)
{
if (item is not ListViewItem listViewItem)
return -1;
ListView listView = (ListView)ItemsControl.ItemsControlFromItemContainer(listViewItem);
return listView.ItemContainerGenerator.IndexFromContainer(listViewItem);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment