有没有办法以编程方式关闭 WPF 中的菜单项 [英] Is there a way to programatically close a menuitem in WPF

查看:20
本文介绍了有没有办法以编程方式关闭 WPF 中的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

谢谢,乔恩

推荐答案

抓住 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;
};

这篇关于有没有办法以编程方式关闭 WPF 中的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆