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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With