Closed uyaem closed 2 years ago
Since this is a major drawback affecting many who follow the standard command approach for context menus, could you please elaborate on how you managed to resolve this via CanExecute by providing the example? Would be really appreciated.
Oh, I see, you @uyaem probably meant that in order to troubleshoot the issue you've attached the CanExecute
method, but it didn't yield any results nonetheless, as it never gets called from what I see.
Just for anyone who comes across this - I was only able to get this to work using the trivial (non routed commands) from the example application in the repository. I had to inject the IoC into the command handler via a parameter allowing the commands to be kind of self-sufficient, otherwise, as per example, they can only react within the Visual Tree which is pretty useless. So if you need commands to work in the Tray Icon context menu - use the approach from here: https://github.com/hardcodet/wpf-notifyicon/tree/master/src/NotifyIconWpf.Sample.ShowCases/Tutorials/04%20-%20ContextMenus
I'm horrible at WPF, commands and "bubbling" events... I have seen many weird things, and usually just take the quirks and their solutions for granted...
I believe the context menu is completely separate from the normal visual tree (it's a popup with it's own root), and thus also has issues with events, commands and even styles.
I'm not sure if this can be made better, but the mentioned tutorial seems to help.
I also found this here: https://stackoverflow.com/questions/59698021/wpf-context-menu-commands-not-setting-isenabled-properly
Closing this, as there is a "solution" and from my point of view we currently have way more pressing things then to fix issues with the way WPF works.
CS
XAML
Noteworthy
IsEnabled
property of the MenuItem remains false.Menu
and works as expected.CanExecute
-method to my CommandBinding, which doesn't get called ever for the tray icon's context menu. This remains the case even when callingCommandManager.InvalidateRequerySuggested()
in e.g. theOpened
event of the ContextMenu.