简体   繁体   English

如何最大化 wpf 中的主要 window

[英]How to maximize main window in wpf

I'm working on WPF app that has multiple windows all connected to main window.我正在开发 WPF 应用程序,它有多个 windows 都连接到主 window。 Also WindowStyle is set to none so i don't have maximize and minimize buttons so I want to be able when i close dependent window to get back main. WindowStyle 也设置为无,所以我没有最大化和最小化按钮,所以我希望能够在我关闭依赖 window 时返回主按钮。 Problem is when i close dependent window main window is still minimized, i have to click on the icon on toolbar to bring it up.问题是当我关闭依赖 window 主要 window 仍然最小化时,我必须单击工具栏上的图标才能将其打开。 What am I doing wrong?我究竟做错了什么?

Here is the code for going to dependent window这是去依赖 window 的代码

private void ButtonVlasnici_Click(object sender, RoutedEventArgs e)
        {
            var win = new WinVlasnici();
            win.Show();
            WindowState = WindowState.Minimized;

        }

and this is for going back to main:这是为了回到主要:

private void ButtonNazad_Click(object sender, RoutedEventArgs e)
        {
            var win = new MainWindow();

            win.WindowState = WindowState.Maximized;
            Close();
        }
var win = new MainWindow();

This is wrong.这是错误的。 You shouldn't be creating new MainWindow from child window's close.您不应该从子窗口的关闭创建新的MainWindow

There are several ways around the problem.有几种方法可以解决这个问题。 If you're using MVVM, which you should (and I can't stress it enough), You can use your dialog handler component to handle the situation.如果您使用的是 MVVM,您应该(而且我不能强调它),您可以使用您的对话处理程序组件来处理这种情况。 One example is available here .此处提供了一个示例。

If you're not using MVVM, you can pass main window's reference to your child window through child's constructor and then use this reference to maximize the main window in child's close event.如果您不使用 MVVM,您可以通过孩子的构造函数将主窗口的引用传递给您的孩子 window,然后使用此引用在孩子的关闭事件中最大化主 window。 Something like this:像这样的东西:

public class Child
  private Window _Main;
  public Child(Window main)
  {
    _Main = main;
  }

  private void ButtonNazad_Click(object sender, RoutedEventArgs e)
  {
    _Main.WindowState = WindowState.Maximized;
    Close();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM