gpt4 book ai didi

c# - 禁用按钮的上下文菜单

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:35 25 4
gpt4 key购买 nike

我想制作一个上下文菜单,当用户在控件(一个按钮)上进行“右键单击”时显示该菜单。不幸的是,一些按钮被禁用了。有人可以帮助我并告诉我如何给他们也提供上下文菜单吗?

我的(不工作)尝试:

        private void ShowRightClickMenu(object sender, MouseEventArgs e)
{
ContextMenu Temp = new ContextMenu();
if (e.Button == MouseButtons.Right && secondTagObj[Convert.ToInt32(((Button)sender).Tag)].typ != string.Empty)
{
this.ContextMenu = Temp; // works
Temp.MenuItems.Add("Create.."); //works
Temp.MenuItems.Add("Delete"); // works
}
if (raster[Convert.ToInt32(((Button)sender).Tag)].Enabled == false && e.Button == MouseButtons.Right)
{
this.ContextMenu = Temp; // works not
Temp.MenuItems.Add("New..."); // works not
}
else
{
this.ContextMenu = Temp; // works, but only if button is visible
Temp.MenuItems.Add("New..."); // works, but only if button is visible
}
}

非常感谢。

最佳答案

(假设这个问题是针对 WPF 的,如@medasocles 回答的评论中所示...)

要使用 WPF 中 @medasocles 提到的 ContextMenuService,您需要执行以下操作:

<Button Content="Blah" Command="{Binding MyCommand}" ContextMenuService.ShowOnDisabled="True">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Context Item" Command="{Binding MyContextCommand}" />
</ContextMenu>
</Button.ContextMenu>
</Button>

不过我不认为这会真正解决您的问题。我认为将按钮放在框架中(如边框)​​并将上下文菜单添加到边框会更容易。然后我会向按钮添加一个触发器,当按钮被禁用时将 IsHitTestVisible 设置为 False ,从而允许交互传递到后面的边框并且您的菜单可以作为预期。

关于c# - 禁用按钮的上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13561149/

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