开发者

Right-Click Context Menu in System Tray

I have a WPF app that runs in the system tray. I'm trying to create a context menu that pops up when you right click on the icon in the tray. Here is the XAML:

<Window.Resources>
        <ContextMenu x:Key="NotifierContextMenu" Placement="MousePoint">
            <MenuItem Header="Exit" Click="Menu_Exit"/>
        </ContextMenu>
    </Window.Resources>

And here is the code-behind:

void NotifyIcon_MouseDown(object sender, System.Windo开发者_开发问答ws.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                var menu = this.FindResource("NotifierContextMenu") as ContextMenu;
                menu.IsOpen = true;
            }
        }

        protected void Menu_Exit(object sender, RoutedEventArgs e)
        {
            NotifyIcon.Visible = false;
            Application.Current.Shutdown();
        }

The issue that I'm having is that when you right-click on the icon, it throws an error that NotifierContextMenu can't be found. What am I missing?


I tried this myself with no problems. Your event handler for the MouseDown is in fact part of the same class that NotifierContextMenu is created right?

Perhaps try making a little code to list out the resources to see if you can match up which resource set it is referring to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜