Problem description
If I make a non-modal window as a child window through setting the Owner of the window to a parent window, and then show a MessageBox
from within this child window, the parent window will lose focus if I close the child window. If windows explorer or another app is open, this app will get the focus and my main window will be hidden.
This seems to be a known problem as I saw it in another newsgroups, but I don’t have seen a good solution. Setting the owner to null in OnDeactivate
is not an option. Setting the owner before showing the MessageBox
to null and resetting after that doesn’t help. Setting the owner to null in the OnClosed
event does also not help.
Simple Solution found
If you experience the same problem as I have described, put the following code in the OnClosing
of all child windows.
void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if (null != Owner) {
Owner.Activate();
}
// ....
}
It can be followed by any further processing logic, even opening MessageBoxes
is tolerated.
Example-Code
The issue seems to be much bigger as I thought. The following example will remove focus of the parent window if the message box will be opened and the the child window will be closed (Copy the code into a loaded event-handler of a Window).
Window firstChildWindow = new Window() {
Title = "Floating Window", Width = 100, Height = 70
};
firstChildWindow.Owner = Window.GetWindow(this);
Button button = new Button() { Content="MessageBox"};
button.Click += delegate { MessageBox.Show("Klicking her breaks the focus-chain."); };
firstChildWindow.Content = button;
firstChildWindow.Show();
Also this example breaks the focus-chain:
Window firstChildWindow = new Window() {
Title = "Floating Window", Width = 100, Height = 70
};
firstChildWindow.Owner = Window.GetWindow(this);
firstChildWindow.Show();
Window secondChildWindow = new Window() { Title="Second Window", Width=100, Height=70};
secondChildWindow.Content = new TextBlock() { Text="SecondWindow"};
secondChildWindow.Owner = firstChildWindow;
secondChildWindow.Show();
Has someone a resolution for this problem. I think about a hack to trigger giving focus to the parent after closing, with Dispatcher or DispachterTimer
or perhaps it would be work to manually force focus to the parent on closed but this all seems to me very unclean (and is also a little complicated if there are more active owned windows of the same parent, as I have in my current app).
No one knows a neat solution to this?
Resources
MSDN Description (see under remarks for non modal windows opened calling Show())
Same problem on MSDN forums without appropriate solution
Please see also: Instable focus of WPF apps
See Question&Answers more detail:os