1

I have a ListView with a ContextMenu attached to it. In the menu handler function I want to know, which item of the ListView was right clicked before the ContextMenu was shown.

How can I do that ?

I'm using WPF and C#

thowa
  • 590
  • 1
  • 8
  • 29

2 Answers2

0

Add a handler to the ListView.ContextMenuOpening event:

MainWindow.xaml:

<Grid>
    <ListView x:Name="_myListView" />
</Grid>

MainWindow.xaml.cs code-behind:

this._myListView.ContextMenuOpening += this._myListView_ContextMenuOpening;

...

void _myListView_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    object selectedItem = this._myListView.SelectedItem;
    // do something with selectedItem...
}
Fredrik
  • 2,247
  • 18
  • 21
0

You could add a MouseRightButtonUp handler to every ListViewItem.

Kim Homann
  • 3,042
  • 1
  • 17
  • 20