Reputation: 4844
I have noticed this very odd behavior in a WPF application.
I have a MainWindow
, which is shown using Show()
from App.OnStartup
. Said MainWindow
can open a (non-modal) SubWindow
, also using Show()
. SubWindow
's Owner
is set to MainWindow
.
When SubWindow
is closed, MainWindow
is visible again (good).
Some actions can cause the SubWindow
to open a third window as a modal dialog, using ShowDialog()
(Owner
is set to SubWindow
). When that modal dialog is opened and closed at least once during the lifetime of a SubWindow
, then the weird thing happens.
After closing SubWindow
, MainWindow
does not come into view. Instead, whatever random window is behind MainWindow
comes into view. Can anyone explain to me why this happens, and how to fix it?
It makes no difference whether the modal dialog is a normal Window
displayed using ShowDialog()
, or a message box shown using MessageBox.Show()
.
Here is some minimal code to reproduce this. Create a new WPF application in visual studio, and paste this into the pre-generated MainWindow.xaml.cs
Then, press a key on the keyboard to open only one window, close it, behavior as expected. Press two keys, close both, then the very first window is behind Visual Studio (presumably).
public MainWindow()
{
InitializeComponent();
this.PreviewKeyDown += (sender, e) =>
{
if (this.Owner is MainWindow)
{
// we're the SubWindow
MessageBox.Show("I am a modal dialog");
// code below produces the exact same behavior as the message box
//var dialog = new MainWindow();
//dialog.Owner = this;
//dialog.ShowDialog();
}
else
{
// we're the initial MainWindow created by App.
var subWindow = new MainWindow();
subWindow.Owner = this;
subWindow.Show();
}
};
}
Upvotes: 16
Views: 7071
Reputation: 21
I realize this is an old thread, but I tried all of these solutions (and many other more in other threads) and none of them worked. I didn't want to set my main window as the subwindow's Owner
because I wanted to be able to minimize the subwindow.
What worked for me was to change the WindowState
property in the main window instead.
This is how I do it, in my mainwindow.cs:
//create new window instance
subWindow = new SubWindow();
//event called on the mainwindow, after the subwindow has been closed.
subWindow.Closed += (sender, args) =>
{
subWindow = null; //clears the variable, so I can re-open the window later
this.WindowState = WindowState.Normal; //this shows the main window!
};
//show subwindow
subWindow.Show();
Upvotes: 1
Reputation: 1
Old question, but I figured I would share what worked for my C# WinForms application since Owner.Focus() did not work for me.
It's a variation on the TopMost fix from Daren, but I found that I don't actually have to set TopMost to true and back. Just setting it to itself does the trick of keeping the parent window from disappearing behind other applications.
protected override void OnClosing(CancelEventArgs e) {
if (Owner != null)
Owner.TopMost = Owner.TopMost;
base.OnClosing(e);
}
Upvotes: 0
Reputation: 780
I had the same problem in WinFoms (.NET 4.7+). My workaround was closing the first dialog first (calling its Close
method) and show the second dialog afterwards.
Example
FirstForm
FirstForm
opens SecondForm
and causes the problem!Workaround
Call Close
method inside the FirstForm
which will be going to open the SecondForm
:
// FirstForm.cs
using(var secondForm = new SecondForm())
{
Close(); // <- this closes FirstForm instance
secondForm.ShowDialog(owner: mainFormInstance);
}
HTH
Upvotes: 0
Reputation: 131
this.Owner.Focus(); is not working for me it still goes to the window behind I had to play around a bit much and tried to used TopMost, the problem with TopMost is I needed to return it to false after the close with it. I used both OnClosing and OnClosed event.
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if (!e.Cancel && this.Owner != null) this.Owner.TopMost = true;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
if (this.Owner != null)
{
this.Owner.TopMost = false;
}
}
Upvotes: 0
Reputation: 329
Hit the same problem only with hiding the window. I can't see that there is an equivalent event to Closing in this situation, but anyway this works:
if (Owner != null) Owner.Focus();
Hide();
Upvotes: 0
Reputation: 942256
This is a pretty annoying WPF bug, I never did find the flaw in the code that causes it but there's a heckofalot of "gotta figure this out" comments in the source code that deals with focusing. Just a workaround, a less than ideal one, you can solve it by explicitly giving the focus to the owner when the window is closing. Copy/paste this code in your SubWindow class;
protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
base.OnClosing(e);
if (!e.Cancel && this.Owner != null) this.Owner.Focus();
}
Upvotes: 13