0

I'm using Visual Studio, WPF, XAML, C#.

I have a button that opens a new Window, TestWindow1. It only allows one instance of the window at a time, not duplicates.

TestWindow1 always stays in front of MainWindow even when the focus is on MainWindow.

How do I bring MainWindow to front on focus?

Or how do I stop TestWindow1 from always being in front?

// Check if Test Window is already open
private Boolean IsTestWindow1Opened = false;

// Open Test Window
private void btnOpenTestWindow1_Click(object sender, RoutedEventArgs e)
{
    // Only allow 1 Window instance
    if (IsTestWindow1Opened) return;

    MainWindow mainwindow = this;

    testwindow1 = new TestWindow1(mainwindow);
    testwindow1.Owner = Window.GetWindow(this);

    testwindow1.Left = Left - 400;
    testwindow1.Top = Top + 0;

    testwindow1.ContentRendered += delegate { IsTestWindow1Opened = true; };
    testwindow1.Closed += delegate { IsTestWindow1Opened = false; };

    testwindow1.Show();
}
Matt McManis
  • 4,475
  • 5
  • 38
  • 93
  • 1
    Check out https://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf?rq=1 – Peter Jun 26 '17 at 16:49

1 Answers1

1

I believe TestWindow1 is always in front due to the line testwindow.Owner = Window.GetWindow(this)

reference: https://msdn.microsoft.com/en-us/library/system.windows.window.owner(v=vs.110).aspx

Edit: Adding on, it is the reason "An owner window can never cover an owned window."

Matthew Wherry
  • 343
  • 2
  • 11