0
votes

I'm working on WPF app that has multiple windows all connected to main 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. 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. What am I doing wrong?

Here is the code for going to dependent 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();
        }
1

1 Answers

1
votes
var win = new MainWindow();

This is wrong. You shouldn't be creating new MainWindow from child window's close.

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