简体   繁体   English

如何获取右键单击打开上下文菜单之前的ListView项

[英]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. 我有一个附加了ContextMenu的ListView。 In the menu handler function I want to know, which item of the ListView was right clicked before the ContextMenu was shown. 在我想知道的菜单处理函数中,在显示ContextMenu之前右键单击ListView的项。

How can I do that ? 我怎样才能做到这一点 ?

I'm using WPF and C# 我正在使用WPF和C#

Add a handler to the ListView.ContextMenuOpening event: 将处理程序添加到ListView.ContextMenuOpening事件:

MainWindow.xaml: MainWindow.xaml:

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

MainWindow.xaml.cs code-behind: MainWindow.xaml.cs的代码隐藏:

this._myListView.ContextMenuOpening += this._myListView_ContextMenuOpening;

... ...

void _myListView_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    object selectedItem = this._myListView.SelectedItem;
    // do something with selectedItem...
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在用户单击上下文菜单中的项目之前,先获得用户在网格中右键单击的行 - Get the Row the user Right Clicked on in a Grid before they click on an item in the Context Menu WPF TabControl上下文菜单在mvvm中右键单击获取项目 - WPF TabControl Context menu get item right-clicked in mvvm 如何通过打开上下文操作菜单从ListView中获取所选项目? (Xamarin.Forms) - How do I get the selected item from a ListView by opening the context actions menu? (Xamarin.Forms) 如何在ListView中获取单击的项目 - How to get clicked item in ListView 如何获得单击的ListView项 - How to Get Clicked ListView Item 如何在WPF上下文菜单项单击事件处理程序中引用右键单击的对象? - How to reference right-clicked object in WPF Context Menu item click event handler? 如何为listView中的每个项目实现上下文菜单 - How to implement a context menu for each item in a listView 在以编程方式填充后单击时,如何获取上下文菜单条中项目的文本? - How can i get the text of an item in context menu strip when clicked after populated programmatically? 右键单击菜单项时如何显示上下文菜单 - How to show a Context Menu when you right click a Menu Item 显示 ListView 中项目的上下文菜单 - Showing a Context Menu for an item in a ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM