private static void WindowActivating(object sender, WindowActivateEventArgs e)
        {
            Trace.WriteLine("WindowActivating");

            if (Keyboard.FocusedElement == null &&
                _lastFocusedElement != null &&
                _lastFocusedElement.IsAlive)
            {
                var elementToSetFocus = _lastFocusedElement.Target as ILayoutElement;
                if (elementToSetFocus != null)
                {
                    var manager = elementToSetFocus.Root.Manager;
                    if (manager == null)
                    {
                        return;
                    }

                    IntPtr parentHwnd;
                    if (!manager.GetParentWindowHandle(out parentHwnd))
                    {
                        return;
                    }

                    if (e.HwndActivating != parentHwnd)
                    {
                        return;
                    }

                    _setFocusAsyncOperation = Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                    {
                        try
                        {
                            SetFocusOnLastElement(elementToSetFocus);
                        }
                        finally
                        {
                            _setFocusAsyncOperation = null;
                        }
                    }), DispatcherPriority.Background);
                }
            }
        }
        private static void WindowActivating(object sender, WindowActivateEventArgs e)
        {
            Trace.WriteLine("WindowActivating");

            if (Keyboard.FocusedElement == null &&
                _lastFocusedElement != null &&
                _lastFocusedElement.IsAlive)
            {
                var elementToSetFocus = _lastFocusedElement.Target as ILayoutElement;
                if (elementToSetFocus != null)
                {
                    var manager = elementToSetFocus.Root.Manager;
                    if (manager == null)
                        return;

                    IntPtr parentHwnd;
                    if (!manager.GetParentWindowHandle(out parentHwnd))
                        return;

                    if (e.HwndActivating != parentHwnd)
                        return;

                    _setFocusAsyncOperation = Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                    {
                        try
                        {
                            SetFocusOnLastElement(elementToSetFocus);
                        }
                        finally
                        {
                            _setFocusAsyncOperation = null;
                        }
                    }), DispatcherPriority.Background);
                }
            }
        }