Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.Windows.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?

like image 212
Justin Avatar asked Nov 05 '22 04:11

Justin


1 Answers

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.

like image 177
CodeWarrior Avatar answered Nov 09 '22 14:11

CodeWarrior