缺少ContextMenu的库

时间:2015-08-21 13:02:22

标签: c# wpf

我使用代码获取触发ContextMenu的控件的名称,但编译器在此行返回此消息:ContextMenu menu = menuItem.GetContextMenu();

  

MenuItem不包含GetContextMenu的定义,也没有找到扩展方法' GetContextMenu'接受类型' MenuItem'的第一个参数。可能缺少using指令或程序集引用。

这里出现同样的错误:

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

我必须添加哪个库?

1 个答案:

答案 0 :(得分:1)

.GetContextMenu()Menu命名空间中System.Windows.Forms类的一种方法(在System.Windows.Forms.dll中)。因此,它不适用于WPF控件......尽管可能会进行一些调整。

如果您在WPF中执行此操作,则需要在ContextMenu命名空间中使用System.Windows.Controls类及其方法。