gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:42:06 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com