取消选中上下文菜单中的项目

发布于 2024-11-05 12:31:27 字数 1023 浏览 4 评论 0原文

我目前在 C# WPF 中有一个 ContextMenu,显示一个 MenuItem。然后,TrayIcon 使用该 ContextMenu。

// menu initialization
private static System.Windows.Controls.ContextMenu trayMenu = new System.Windows.Controls.ContextMenu();

// menu item initialization
private static System.Windows.Controls.MenuItem displayOnScreenControls = new System.Windows.Controls.MenuItem();
displayOnScreenControls.Header = "Display presenter controls";
displayOnScreenControls.IsCheckable = true;
displayOnScreenControls.Checked += new RoutedEventHandler(displayOnScreenControls_Checked);
displayOnScreenControls.Unchecked += new RoutedEventHandler(displayOnScreenControls_Unchecked);

// add icons to the tray menu
trayMenu.Items.Add(displayOnScreenControls);

“displayOnScreenControls”项目是可选的。当用户选中此选项并满足某些条件时,将出现一个控制窗口。

如果用户选择关闭此窗口,则表明他们不再希望打开控制窗口。为了保持一致性,我需要取消选中“displayOnScreenControls”菜单项——否则该项目将被选中,但窗口不会打开(如果用户想要重新启用窗口等,这会让用户感到困惑)。

知道如何处理这个问题吗?我基本上需要取消选中菜单项。我可以解构菜单并重建它,但这似乎是浪费时间。

与往常一样,提前感谢您的任何帮助。

I currently have a ContextMenu in C# WPF, displaying a MenuItem. This ContextMenu is then utilized by a TrayIcon.

// menu initialization
private static System.Windows.Controls.ContextMenu trayMenu = new System.Windows.Controls.ContextMenu();

// menu item initialization
private static System.Windows.Controls.MenuItem displayOnScreenControls = new System.Windows.Controls.MenuItem();
displayOnScreenControls.Header = "Display presenter controls";
displayOnScreenControls.IsCheckable = true;
displayOnScreenControls.Checked += new RoutedEventHandler(displayOnScreenControls_Checked);
displayOnScreenControls.Unchecked += new RoutedEventHandler(displayOnScreenControls_Unchecked);

// add icons to the tray menu
trayMenu.Items.Add(displayOnScreenControls);

The "displayOnScreenControls" item is checkable. When the user has this option checked and certain conditions are met, a control window will appear.

If the user choses to close this window, this signals they no longer want the control window open. To maintain consistency, I need to uncheck the "displayOnScreenControls" menu item -- otherwise the item will be checked but the window will not be open (which will be confusing for the user if they want to reenable the window, etc).

Any idea on how to handle this? I need to essentially uncheck the menuitem. I could deconstruct the menu and reconstruct it, but that seems like a waste of time.

As always, thanks in advance for any assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2024-11-12 12:31:27

所以您只想在窗口关闭时以编程方式取消选中菜单项?您需要做的就是设置 IsChecked

displayOnScreenControls.IsChecked = false;

将其放入您的 Window.Closed 事件处理程序中你应该表现得很好。

So you just want to programmatically uncheck the menu item when your Window is closed? All you need to do is set IsChecked.

displayOnScreenControls.IsChecked = false;

Put that in your Window.Closed event handler and you should be good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文