当用户单击菜单外部时,如何使 WPF 上下文菜单消失?

发布于 2024-10-30 01:38:31 字数 1169 浏览 0 评论 0原文

一、标准信息:

VS2010 Ultimate
Win7 旗舰版 x64
WPF 应用程序

WPF 上下文菜单:

    <ContextMenu x:Key="RightClickSystemTray" Placement="MousePoint">
        <MenuItem Header="Exit" Click="Menu_Exit"></MenuItem>
    </ContextMenu>

显示它的代码:

    void _notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            ContextMenu menu = (ContextMenu)this.FindResource("RightClickSystemTray");
            menu.IsOpen = true;
        }
    }

上下文菜单是用 XAML 构建的 WPF ContextMenu。系统托盘中的通知图标是一个表单通知图标(我不知道本机 WPF 通知图标)。因此,不使用 notificationicon.ContextMenu 属性。上面的代码工作正常。当用户右键单击通知图标时,上下文菜单将按其应有的方式显示。

我遇到的问题是让 ContextMenu 在我想要的时候消失。只要您单击 WPF 应用程序中的某个位置,它就会消失。此行为是自动的。但是,如果用户单击其他位置(例如任务栏),菜单不会消失。 “LostFocus”事件不会触发,因为这些类型的事件仅在某个元素失去焦点到同一应用程序中的另一个元素时触发。就应用程序而言,ContextMenu 永远不会失去焦点。 “停用”是我尝试使用的另一个事件。我应该澄清这一点,应用程序有一个“关闭托盘”选项,因此应用程序可以在用户有机会右键单击并显示菜单之前关闭并触发停用事件。当菜单出现时,该应用程序不会重新激活,因此当我单击任务栏时,即使已停用的应用程序也不会触发。

最后,问题来了。当用户单击远离上下文菜单时,如何使上下文菜单消失,即使用户单击的位置不在创建上下文菜单的应用程序中?

First, the standard info:

VS2010 Ultimate
Win7 Ultimate x64
WPF app

The WPF context menu:

    <ContextMenu x:Key="RightClickSystemTray" Placement="MousePoint">
        <MenuItem Header="Exit" Click="Menu_Exit"></MenuItem>
    </ContextMenu>

The code to show it:

    void _notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            ContextMenu menu = (ContextMenu)this.FindResource("RightClickSystemTray");
            menu.IsOpen = true;
        }
    }

The context menu is a WPF ContextMenu built in XAML. The notify icon in the system tray is a Forms notify icon (I don't know of a native WPF notify icon). Therefore, the notifyicon.ContextMenu property is not used. The code above works fine. When the user right clicks on the notify icon, the context menu shows as it should.

The issue I'm having is with making the ContextMenu go away when I want. It goes away fine as long as you click somewhere within the WPF app. This behavior is automatic. But if the user clicks elsewhere, such as the taskbar, the menu doesn't disappear. "LostFocus" events don't fire because these types of events only fire when an element loses focus to another element within the same app. As far as the app is concerned, the ContextMenu never loses focus. "Deactivated" was another event I tried to use. I should clarify at this point that the application has a "close to tray" option, so the application could close and fire the deactivated event before the user has a chance to right click and show the menu. The app is not reactivated when the menu appears, so the deactivated even won't fire when I click on the taskbar.

So finally, the question. How do I get my context menu to disappear when the user clicks away from it, even if the place where the user clicks isn't in the application that created the context menu?

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

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

发布评论

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

评论(2

水波映月 2024-11-06 01:38:31

我解决这个问题的方法只是在打开菜单时激活主窗口。

添加 this.Activate() 会在上下文菜单打开后激活 mainWindow,这样当 mainWindow 被停用(在外部单击)时,它会自动将 NotifyIcon 的 ContextMenu 的 IsOpen 属性设置为 false。

我的项目是C# WPF、.NET Framework3.5项目。

void _notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
     if (e.Button == System.Windows.Forms.MouseButtons.Right)
     {
         ContextMenu menu =(ContextMenu)this.FindResource("RightClickSystemTray");
         menu.IsOpen = true;

         // this activates the mainWindow so that when it's de-activated(clicked outside of) it automatically sets the NotifyIcon's ContextMenu's IsOpen property to false
         this.Activate();
     }
}

What I did to fix this was simply to activate the mainWindow when the menu is opened.

Adding this.Activate() activates the mainWindow after the context menu has been opened so that when the mainWindow is de-activated(clicked outside of) it automatically sets the NotifyIcon's ContextMenu's IsOpen property to false.

My project was a C# WPF, .NET Framework3.5 project.

void _notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
     if (e.Button == System.Windows.Forms.MouseButtons.Right)
     {
         ContextMenu menu =(ContextMenu)this.FindResource("RightClickSystemTray");
         menu.IsOpen = true;

         // this activates the mainWindow so that when it's de-activated(clicked outside of) it automatically sets the NotifyIcon's ContextMenu's IsOpen property to false
         this.Activate();
     }
}
笑看君怀她人 2024-11-06 01:38:31

我不知道这是否是一个可行的选择,但如果您切换到这个库,您'将为 WPF 提供一个集成良好的托盘图标,而不会出现此类问题(还有 ContextMenus 的示例代码)

I do not know if this is a viable option but if you switched to this library you'll have a well integrated tray-icon for WPF without such issues (there is sample code there for ContextMenus as well)

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