I have a button inside the MenuItem header. Clicking on a menu item (and not part of a button) closes the menu as expected, but pressing the button does not close the menu, and I want it. How can i fix this?
More background:
I have two commands that are secondary to each other, and I want to put them on the menu. The first option will be used most of the time, so I do not want two menu items to occupy space, and I do not want to put them in a submenu. Thus, I clicked on the idea of ββplacing a button for the secondary command inside MenuItem, for example:
<Menu> <MenuItem Header="Git"> <MenuItem Command="{Binding PrimaryCommand}"> <MenuItem.Header> <StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" Text="Rebase "/> <Button Content="Interactive" Focusable="False" Command="{Binding SecondaryCommand}"/> </StackPanel> </MenuItem.Header> </MenuItem> </MenuItem> </Menu>
which is as follows:
http://www.excastle.com/misc/wpf-menuitem-with-button.png
So, if I press the button, I will get a secondary command; but if I click anywhere in the menu item, I will get the basic command. It works well, down to the point.
The problem is that I click on MenuItem or Button, I made my choice, and I want the menu to close, but if I press the button, it will not close the menu. The menu remains open. In some cases, I see that this is really useful behavior (for example, the zoom in / out buttons, which you can click several times without closing the menu after each click, as in the Google Chrome menu); but in this case I want the button to close the menu.
In case someone wonders about this Focusable="False" , deleting it does not fix the problem, it just changes it so that the button draws with a blue (focused) frame for the second time you drop the menu.
I tried using a hyperlink instead of a button with the same effect: clicking on the hyperlink does not close the menu.
How to force the built-in button to close the menu?