我正在使用代码获取触发 ContextMenu 的控件的名称,但编译器在此行返回此消息:ContextMenu menu = menuItem.GetContextMenu();
MenuItem does not contain a definition of GetContextMenu and found no extension method 'GetContextMenu' accepting a first argument of type 'MenuItem'. Probably missing a using directive or an assembly reference.
同样的错误:
Control sourceControl = menu.SourceControl;
这是我的方法:
private void ClearTable_Click(object sender, RoutedEventArgs e)
{
// Try to cast the sender to a MenuItem
MenuItem menuItem = sender as MenuItem;
if (menuItem != null)
{
// Retrieve the ContextMenu that contains this MenuItem
ContextMenu menu = menuItem.GetContextMenu();
// Get the control that is displaying this context menu
Control sourceControl = menu.SourceControl;
}
}
我必须添加哪个库?
最佳答案
.GetContextMenu()
是 System.Windows.Forms
命名空间(在 System.Windows.Forms. dll)。因此,它并非设计用于 WPF 控件......尽管通过一些调整它可能是可能的。
如果您在 WPF 中执行此操作,则需要使用 System.Windows.Controls
命名空间中的 ContextMenu
类及其方法。
关于c# - 缺少上下文菜单的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32141025/