-
-
Notifications
You must be signed in to change notification settings - Fork 909
fix: Make NotifyIcon contextmenu display properly
#1534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
use x:Name=MainWindow is better, but i want less code. |
| TooltipText="WPF UI Gallery"> | ||
| <tray:NotifyIcon.Menu> | ||
| <ContextMenu ItemsSource="{Binding ViewModel.TrayMenuItems, Mode=OneWay}" /> | ||
| <ContextMenu DataContext="{Binding DataContext, Source={x:Reference NavigationView}}" ItemsSource="{Binding ViewModel.TrayMenuItems, Mode=OneWay}" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think x:Reference is discouraged, but I'm only not sure after reading https://learn.microsoft.com/en-us/dotnet/desktop/xaml-services/xreference-markup-extension#xreference-and-wpf.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<tray:NotifyIcon
FocusOnLeftClick="True"
MenuOnRightClick="True">
<tray:NotifyIcon.Resources>
<DiscreteObjectKeyFrame x:Key="proxy" Value="{Binding}"/>
</tray:NotifyIcon.Resources>
<tray:NotifyIcon.Menu>
<ContextMenu DataContext="{Binding Source={StaticResource proxy},Path=Value}">
<ui:MenuItem
Name="OpenSettings"
Command="{Binding OpenSettingsCommand}"
CommandParameter="123"
Header="{x:Static str:Resources.AppSetting}" />
<ui:MenuItem
Name="Exit"
Command="{Binding ExitAppCommand}"
Header="{x:Static str:Resources.AppExit}" />
</ContextMenu>
</tray:NotifyIcon.Menu>
</tray:NotifyIcon>ok,i have plan b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chucker
I have provided two of my common practices. My approach is to solve it through XAML:
- Using x:Referenceto traverse the visual tree.
- Using a lazy-loading object (I directly borrowed the DiscreteObjectKeyFramefrom the WPF animation system).
Can we discuss your thoughts? What fixes are planned?
Maybe we could start by getting something done, even if it's not the final solution? The Wpf.Ui.Gallery is a sample project, so we can always change it later if we think of a better way.
NotifyIcon contextmenu display properly
Pull request type
Please check the type of change your PR introduces:
What is the current behavior?
Issue Number: N/A
NotifyIcon's ContextMenu not display
What is the new behavior?
Other information
The context menu isn't part of the same visual tree and therefore doesn't have the same DataContext.
I have provided a tip here to solve this problem.