简体   繁体   中英

How to Bring MainWindow to Front on Focus?

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

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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