Weird behavior with contextual menu not rendering properly.
-
I have a button with a contextual menu attached to it, I want display the menu on the left click instead of the default right click. I'm using a company wide "resource pack" that defines some style. When I left click on the button to open the menu, the menu is drawn/rendered using the default windows style and the event/commands are not triggered. When I right click on the button to open the menu, the menu is drawn/rendered using our styles and events/commands are working. When I left click again the menu is drawn/rendered using our styles and event/commands are working I'm not sure why the wrong menu is loaded once. Any insights or things I might have missed ?
private void ButtonBase\_OnClick(object sender, RoutedEventArgs e) { if (MyContextMenu.ContextMenu != null) MyContextMenu.ContextMenu.IsOpen = true; }
CI/CD = Continuous Impediment/Continuous Despair
-
I have a button with a contextual menu attached to it, I want display the menu on the left click instead of the default right click. I'm using a company wide "resource pack" that defines some style. When I left click on the button to open the menu, the menu is drawn/rendered using the default windows style and the event/commands are not triggered. When I right click on the button to open the menu, the menu is drawn/rendered using our styles and events/commands are working. When I left click again the menu is drawn/rendered using our styles and event/commands are working I'm not sure why the wrong menu is loaded once. Any insights or things I might have missed ?
private void ButtonBase\_OnClick(object sender, RoutedEventArgs e) { if (MyContextMenu.ContextMenu != null) MyContextMenu.ContextMenu.IsOpen = true; }
CI/CD = Continuous Impediment/Continuous Despair
The WPF
ContextMenu
doesn't inherit theDataContext
of its parent element. You need to set that explicitly for the bindings to work:<ContextMenu x:Name="MyContextMenu" DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
As for the styles not being applied when you left-click, have you tried different sequences? Try two left-clicks, or a right-click followed by a left click, to see whether the styles are applied the second time the menu appears, or only applied after a right-click.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The WPF
ContextMenu
doesn't inherit theDataContext
of its parent element. You need to set that explicitly for the bindings to work:<ContextMenu x:Name="MyContextMenu" DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
As for the styles not being applied when you left-click, have you tried different sequences? Try two left-clicks, or a right-click followed by a left click, to see whether the styles are applied the second time the menu appears, or only applied after a right-click.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Adding the datacontext does not change anything. The menu loads correctly the first when I right click on the button; after that it loads correctly on the left click. It's not urgent for now, I'm prototyping a UI. , but slightly annoying. Thanks.
CI/CD = Continuous Impediment/Continuous Despair