@@ -69,7 +69,8 @@ public class DialogHost : ContentControl
6969 private DialogOpenedEventHandler _attachedDialogOpenedEventHandler ;
7070 private DialogClosingEventHandler _attachedDialogClosingEventHandler ;
7171 private object _closeDialogExecutionParameter ;
72- private IInputElement _restoreFocus ;
72+ private IInputElement _restoreFocusDialogClose ;
73+ private IInputElement _restoreFocusWindowReactivation ;
7374 private Action _currentSnackbarMessageQueueUnPauseAction = null ;
7475 private Action _closeCleanUp = ( ) => { } ;
7576
@@ -237,8 +238,8 @@ public object Identifier
237238
238239 private static void IsOpenPropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
239240 {
240- var dialogHost = ( DialogHost ) dependencyObject ;
241-
241+ var dialogHost = ( DialogHost ) dependencyObject ;
242+
242243 if ( dialogHost . _popupContentControl != null )
243244 ValidationAssist . SetSuppress ( dialogHost . _popupContentControl , ! dialogHost . IsOpen ) ;
244245 VisualStateManager . GoToState ( dialogHost , dialogHost . SelectState ( ) , ! TransitionAssist . GetDisableTransitions ( dialogHost ) ) ;
@@ -260,12 +261,15 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
260261 dialogHost . _session . IsEnded = true ;
261262 dialogHost . _session = null ;
262263 dialogHost . _closeCleanUp ( ) ;
263-
264+ dialogHost . Dispatcher . InvokeAsync ( ( ) => dialogHost . _restoreFocusDialogClose . Focus ( ) , DispatcherPriority . Input ) ;
265+
264266 return ;
265267 }
266268
267269 dialogHost . _asyncShowWaitHandle . Reset ( ) ;
268270 dialogHost . _session = new DialogSession ( dialogHost ) ;
271+ var window = Window . GetWindow ( dialogHost ) ;
272+ dialogHost . _restoreFocusDialogClose = window != null ? FocusManager . GetFocusedElement ( window ) : null ;
269273
270274 //multiple ways of calling back that the dialog has opened:
271275 // * routed event
@@ -507,7 +511,7 @@ public static DialogClosingEventHandler GetDialogClosingAttached(DependencyObjec
507511 }
508512
509513 public static readonly DependencyProperty DialogClosingCallbackProperty = DependencyProperty . Register (
510- nameof ( DialogClosingCallback ) , typeof ( DialogClosingEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogClosingEventHandler ) ) ) ;
514+ nameof ( DialogClosingCallback ) , typeof ( DialogClosingEventHandler ) , typeof ( DialogHost ) , new PropertyMetadata ( default ( DialogClosingEventHandler ) ) ) ;
511515
512516 /// <summary>
513517 /// Callback fired when the <see cref="DialogClosing"/> event is fired, allowing the event to be processed from a binding/view model.
@@ -678,16 +682,16 @@ private static void WatchWindowActivation(DialogHost dialogHost)
678682
679683 private void WindowOnDeactivated ( object sender , EventArgs eventArgs )
680684 {
681- _restoreFocus = _popup != null ? FocusManager . GetFocusedElement ( ( Window ) sender ) : null ;
685+ _restoreFocusWindowReactivation = _popup != null ? FocusManager . GetFocusedElement ( ( Window ) sender ) : null ;
682686 }
683687
684688 private void WindowOnActivated ( object sender , EventArgs eventArgs )
685689 {
686- if ( _restoreFocus != null )
690+ if ( _restoreFocusWindowReactivation != null )
687691 {
688692 Dispatcher . BeginInvoke ( new Action ( ( ) =>
689693 {
690- Keyboard . Focus ( _restoreFocus ) ;
694+ Keyboard . Focus ( _restoreFocusWindowReactivation ) ;
691695 } ) ) ;
692696 }
693697 }
0 commit comments