I have a menu in wpf that has an input box and a button on it. Once the user clicks the button I need to close the menu.
Is there a way to do this?
I find that using IsSubmenuOpen
doesn't properly eliminate focus from the Menu containing the MenuItem
(especially if the Menu is in a ToolBar - the top-level MenuItem
remains Selected
even though the menu is "Closed"). I find sending a MouseUp
event to the MenuItem
works better (in the button's, or nested control's, Click event handler):
private void button_Click(object sender, RoutedEventArgs e) {
Button b = sender as Button;
if (b == null || !(b.Parent is MenuItem))
return;
MenuItem mi = b.Parent as MenuItem;
mi.RaiseEvent(
new MouseButtonEventArgs(
Mouse.PrimaryDevice, 0, MouseButton.Left
)
{RoutedEvent=Mouse.MouseUpEvent}
);
}