【问题标题】:how to enable disable menu item of main window from tab buton如何从选项卡按钮启用禁用主窗口的菜单项
【发布时间】:2018-10-21 22:07:42
【问题描述】:

在 WPF 表单应用程序中

Menu 和 MenuItems 位于主窗口窗体上,默认情况下我将其禁用

然后在作为登录表单的选项卡上打开

MainWindow 和 Login Tab 是不同的表单,登录表单在位于 MainWindow 的 TabControl 中打开

现在我希望当我从登录表单登录时,在成功登录时应启用 Menu 和 MenuItems。

附上截图供参考 enter image description here 请分享相同的代码

提前致谢

【问题讨论】:

    标签: wpf forms tabs tabcontrol menu-items


    【解决方案1】:

    当您从 MainWindow 创建新的登录选项卡时,您可以将整个 MainWindow 控件 (this) 作为参数传递给登录选项卡。

    从那里您将能够从您的登录选项卡中调用主窗口内的项目。

    tabPage1.Enabled = false; // this disables the controls on it
    tabPage1.Visible = false; // this hides the controls on it.
    

    下面的代码取自两个独立的窗口,第一个称为 MainWindow,它有一个按钮,可以打开一个新窗口,我称之为 Window1。

    主窗口代码:

    private void ButtonNewPage_Click(object sender, RoutedEventArgs e)
        {
            Window1 newWindow = new Window1(this);
            this.Hide();
            newWindow.Show();
        }
    

    Window1 代码,注意在 Window1 Constructor 中它需要一个 MainWindow 类型的参数:

    public partial class Window1 : Window
    {
        private MainWindow parent;
    
        public Window1(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }
    
        private void ButtonOldPage_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
            parent.Show();
        }
    }
    

    *编辑显示将旧窗口传递给新窗口

    【讨论】:

    • 您能否分享代码以将 MainWindow 控件作为参数传递到主窗口和标签页中,我对此感到困惑
    • 我在原帖中添加了关于传递窗口的说明
    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多