Is there a way to programatically close a menuitem in WPF

后端 未结 3 1316
执笔经年
执笔经年 2021-01-12 06:28

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?

         


        
相关标签:
3条回答
  • 2021-01-12 06:47

    Steve thanks for your solution. That is actually right answer, and finally something that really works beside of tons of bad answers over the internet. I have a shorter (and more safe) solution based on your anwser. Because direct parent (e.Parent) of the button is not always MenuItem (from original answer that is StackPanel), your solution will not work. So just set the Name property of the MenuItem (Name="MyMenuItem") and hook this handler on the Button:

        private void Button_Click(object sender, RoutedEventArgs e) {
            MyMenuItem.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) {
                RoutedEvent = Mouse.MouseUpEvent
            });
        }
    
    0 讨论(0)
  • 2021-01-12 07:04

    Get hold of the MenuItem and do:

    _menuItem.IsSubmenuOpen = false;
    

    Easy way to get hold of it:

    <Button x:Name="_button" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=2}"/>
    

    Code-behind:

    _button.Click += delegate
    {
        (_button.Tag as MenuItem).IsSubmenuOpen = false;
    };
    
    0 讨论(0)
  • 2021-01-12 07:04

    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}
               );
            }
    
    0 讨论(0)
提交回复
热议问题