WPF App loses completely focus 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 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)
  if (null != Owner) {
  // ....

It can be followed by any further processing logic, even opening MessageBoxes is tolerated.


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;

Also this example breaks the focus-chain:

Window firstChildWindow = new Window() {
    Title = "Floating Window", Width = 100, Height = 70
firstChildWindow.Owner = Window.GetWindow(this);

Window secondChildWindow = new Window() { 
    Title="Second Window",Width=100,Height=70};
secondChildWindow.Content = new TextBlock() { 
secondChildWindow.Owner = firstChildWindow;

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 manualy 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?


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).

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.


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();
                (sender as Window).Owner.Focus();
            (sender as Window).Owner = null;

Need Your Help

Scope functions apply/with/run/also/let: Where do the names come from?

lambda kotlin higher-order-functions scoping

There are quite a few blog posts (like this) on usages of the standard library functions apply/with/run/also/let available that make it a bit easier to distingish when to actually use which of those