有没有办法以编程方式关闭 WPF 中的菜单项

发布于 2024-07-17 09:41:58 字数 585 浏览 6 评论 0原文

我在 wpf 中有一个菜单,上面有一个输入框和一个按钮。 一旦用户单击按钮,我需要关闭菜单。

有没有办法做到这一点?

    <Menu x:Name="MainMenu">
        <MenuItem Header="Main">
            <MenuItem Header="SubMenu" x:Name="SubMenu">
                <StackPanel Orientation="Horizontal">
                    <TextBox Width="50" x:Name="TextBox" />
                    <Button Content="Click Me and Close" x:Name="Button" IsDefault="True"/>
                </StackPanel>
            </MenuItem>
        </MenuItem>

谢谢, 乔恩

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?

    <Menu x:Name="MainMenu">
        <MenuItem Header="Main">
            <MenuItem Header="SubMenu" x:Name="SubMenu">
                <StackPanel Orientation="Horizontal">
                    <TextBox Width="50" x:Name="TextBox" />
                    <Button Content="Click Me and Close" x:Name="Button" IsDefault="True"/>
                </StackPanel>
            </MenuItem>
        </MenuItem>

Thanks,
Jon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心头的小情儿 2024-07-24 09:41:58

获取 MenuItem 并执行以下操作:

_menuItem.IsSubmenuOpen = false;

获取它的简单方法:

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

代码隐藏:

_button.Click += delegate
{
    (_button.Tag as MenuItem).IsSubmenuOpen = false;
};

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;
};
兔姬 2024-07-24 09:41:58

我发现使用 IsSubmenuOpen 并不能正确消除包含 MenuItem 的菜单的焦点(特别是如果菜单位于工具栏中 - 顶级 MenuItem<即使菜单“关闭”,/code> 仍保持选定)。 我发现将 MouseUp 事件发送到 MenuItem 效果更好(在按钮或嵌套控件的 Click 事件处理程序中):

        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}
           );
        }

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}
           );
        }
初相遇 2024-07-24 09:41:58

史蒂夫感谢您的解决方案。 这实际上是正确的答案,并且最终是除了互联网上大量错误答案之外真正有效的答案。 根据您的答案,我有一个更短(更安全)的解决方案。 因为按钮的直接父级(e.Parent)并不总是 MenuItem(来自原始答案,即 StackPanel),所以您的解决方案将不起作用。 因此,只需设置 MenuItem 的 Name 属性 (Name="MyMenuItem") 并将此处理程序挂接到 Button 上:

    private void Button_Click(object sender, RoutedEventArgs e) {
        MyMenuItem.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) {
            RoutedEvent = Mouse.MouseUpEvent
        });
    }

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
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文