WPF App loses focus completely on window close
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 hidd开发者_如何学Cen.
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
What most likely happened here is you have two independent top level windows and closed one of them. This will sometimes cause focus to jump to another application.
This also happens if one windows owns a second, which in turn owns a third. Probable BUG in the Win32 API but its been with us forever so good luck getting it fixed.
The workaround is to manually hand focus back to child in the Closing event of the grandchild. But in this case, grandchild is a messagebox so you can't do that. The easiest thing to do is to own messagebox to parent. The next easiest is to make your own messagebox control.
This is a very annoying bug, the most common solution provided is:
call Me.Owner.Focus
in the Window_Unloaded
or Window_Closing
event.
But that still doesnt work 100%, you still see the background-window flashing to the foreground (very briefly), before focus is restored correctly.
I found a method which works better:
call Me.Owner.Activate
before Me.Close
(or in the _Closing
event).
2020 and still in trouble...
Workaround for Wpf :
public ContactsWindow()
{
InitializeComponent();
Closing += ContactsWindow_Closing;
}
private void ContactsWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Owner?.Activate();
}
It seems to work for me, interested in any news
Maybe i am quite late on the matter.
@HCL this problem could be resolved more easily by setting the options parameter of the MessageBox.show() to MessageBoxOptions.None.
Cheers...
it is possible to write your own messagebox, you can implement it as a floater the disable entire screen when showed.
you check the caliburn mvvm frameork sample, there is a nice implementation of messagebox as service.
Ownership has nothing to do with which window activation. To see this, change the owner of the second window to the top window and run the program. Notice that nothing changes. Ownership has to do with "If a window is minimized what other windows need to be minimized". So you are stuck with using custom activation code.
Johsua's .Activate (see above) solution works quite nicely with all sorts of window types. At least it did the trick for me.
i had a similar problem. I had a one main window, where rest child windows was assign to that window via Owner property. If main window had two or more children, after close second child, app lose focus. I used trick with child.Owner = null before close, but then my next child window lose focus. I handled it by getting all children and set focus to the last one before closing child. Here is code example:
private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
{
var childWindows = (sender as Window).Owner.OwnedWindows;
if (childWindows.Count - 2 >= 0)
childWindows[childWindows.Count - 2].Focus();
else
{
(sender as Window).Owner.Focus();
}
(sender as Window).Owner = null;
}
精彩评论