c# - 缺少上下文菜单的库

标签 c# wpf

我正在使用代码获取触发 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/

相关文章:

c# - 如何在调试应用程序期间定义事务超时

c# - 是否可以在加载内容之前更改 WebBrowser 控件的背景颜色?

c# - 计算硬盘吞吐量

c# - 基于常量的 MSBuild 条件

c# - 用 using(...) { } 语句包装 Application.Run(new Form()) 是否安全?

wpf - 在WPF窗口中动态更改内容

c# - 将计算属性添加到 wcf 客户端以进行绑定(bind)

wpf - 在 FullRow 选择模式下禁用 DataGrid 当前单元格边框

c# - 混淆相对路径和绝对路径

c# - Generic.xaml - 引用样式