Reputation: 4675
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();
}
Upvotes: 0
Views: 87
Reputation: 353
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."
Upvotes: 1