'wpf get screen position of selected item in listview

In the mouseup event for a listview, how do I get the screen position of a selected item? I can get the screen position of the listview itself (.pointtoscreen) but can't find a way to determine the screen position of a selected item.

I've reviewed other SO articles but didn't find anything specific to items in the listview.

wpf


Solution 1:[1]

You can handle the ListBox.SelectionChanged event (or Selector.Selected). Then get the container of the selected item to calculate its coordinates:

partial class MainWindow : Window
{
  private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    var listBox = sender as ListBox;
    var selectedItemContainer = listBox.ItemContainerGenerator.ContainerFromItem(listBox.SelectedItem) as UIElement;
    var pointOnSelectedItem = new Point(); // top-left corner

    var pointOnScreen = selectedItemContainer.PointToScreen(pointOnSelectedItem);
    var pointRelativeToWindow = selectedItemContainer.TranslatePoint(pointOnSelectedItem, this);
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BionicCode