gpt4 book ai didi

c# - 单击时 WPF ContextMenu 消失

转载 作者:行者123 更新时间:2023-11-30 22:04:27 28 4
gpt4 key购买 nike

我有一个 WPF 应用程序,我为网格添加了一个 ContextMenu。用户在网格上进行选择,然后;出现上下文菜单。我在 ContextMenu 上有一些文本框,用户可以在其中输入一些值,但是如果用户单击 ContextMenu 本身(而不是在文本框内),对话框就会消失。我想阻止这种情况,并尝试获取一些事件来指示何时单击 ContextMenu。

private void CreateContextMenu()
{
detectionInfoContextMenu = new ContextMenu(); //create an instance of the class
//selectionBoxCanvas.ContextMenu = detectionInfoContextMenu;
playVideoGrid.ContextMenu = detectionInfoContextMenu;
detectionInfoContextMenu.MouseDown += detectionInfoContextMenu_MouseDown;
}

void detectionInfoContextMenu_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
MessageBox.Show("You clicked me!");
}

我正在尝试获取鼠标按钮事件以确定是否单击了鼠标左键。这似乎在其他控件上工作得很好,例如canvas 等,但在 ContextMenu 上似乎不起作用。我使用了错误的事件吗?

最佳答案

您可以使用 StaysOpenOnClick 来防止关闭菜单:

<MenuItem StaysOpenOnClick="True">Test</MenuItem>

或者您可以像 Ivan Zub 在评论中建议的那样使用 Popup

这里是菜单中带有 TextBox 的示例:

<ContextMenu>
<MenuItem StaysOpenOnClick="True">
<MenuItem.Header>
<TextBox Width="100" />
</MenuItem.Header>
</MenuItem>
</ContextMenu>

关于c# - 单击时 WPF ContextMenu 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239921/

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