I have a Button
inside a MenuItem.Header
like this:
<Menu>
<MenuItem>
<MenuItem.Header>
<Button>Hello</Button>
</MenuItem.Header>
<MenuItem Header="SubItem1"/>
<MenuItem Header="SubItem2"/>
</MenuItem>
</Menu>
if I click the MenuItem
outside the Button
, the sub-menu opens. But if I click the Button
the sub-menu will not open. I believe that's because the event of the clicked is not passed to the MenuItem
. How do I fix it?
In short - I want the sub-menu to open when clicking the Button
.
(The use is mainly for styling purposes, I have a button style and I want to use it as a MenuItem
)
A Button
doesn't know how to expand a MenuItem
unless you tell it how to by writing some code:
<Menu>
<MenuItem>
<MenuItem.Header>
<Button Click="Button_Click">Hello</Button>
</MenuItem.Header>
<MenuItem Header="SubItem1"/>
<MenuItem Header="SubItem2"/>
</MenuItem>
</Menu>
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
MenuItem mi = btn.Parent as MenuItem;
if (mi != null)
mi.IsSubmenuOpen = !mi.IsSubmenuOpen;
}