Main window disappears behind other application's windows after a sub window uses ShowDialog on a third window
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(); }
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();
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; }}