简体   繁体   English

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

[英]Unchecking item in a context menu

I currently have a ContextMenu in C# WPF, displaying a MenuItem. 我目前在C#WPF中有一个ContextMenu,显示一个MenuItem。 This ContextMenu is then utilized by a TrayIcon. 然后,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);

The "displayOnScreenControls" item is checkable. “ displayOnScreenControls”项是可检查的。 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). 为了保持一致性,我需要取消选中“ displayOnScreenControls”菜单项-否则将选中该项目,但不会打开窗口(如果用户要重新启用窗口等,将会使用户感到困惑)。

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. 与往常一样,在此先感谢您的协助。

So you just want to programmatically uncheck the menu item when your Window is closed? 因此,您只想在关闭窗口时以编程方式取消选中菜单项吗? All you need to do is set IsChecked . 您所需要做的就是设置IsChecked

displayOnScreenControls.IsChecked = false;

Put that in your Window.Closed event handler and you should be good. 将其放在Window.Closed事件处理程序中,您应该会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM