简体   繁体   中英

How to get the ListView Item that was right clicked before opening the context menu

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#

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...
}

您可以向每个ListViewItem添加MouseRightButtonUp处理程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM