取消选中上下文菜单中的项目
我目前在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以您只想在窗口关闭时以编程方式取消选中菜单项?您需要做的就是设置 IsChecked。
将其放入您的 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.
Put that in your Window.Closed event handler and you should be good.