@@ -69,7 +69,8 @@ public class DialogHost : ContentControl
69
69
private DialogOpenedEventHandler _attachedDialogOpenedEventHandler ;
70
70
private DialogClosingEventHandler _attachedDialogClosingEventHandler ;
71
71
private object _closeDialogExecutionParameter ;
72
- private IInputElement _restoreFocus ;
72
+ private IInputElement _restoreFocusDialogClose ;
73
+ private IInputElement _restoreFocusWindowReactivation ;
73
74
private Action _currentSnackbarMessageQueueUnPauseAction = null ;
74
75
private Action _closeCleanUp = ( ) => { } ;
75
76
@@ -237,8 +238,8 @@ public object Identifier
237
238
238
239
private static void IsOpenPropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
239
240
{
240
- var dialogHost = ( DialogHost ) dependencyObject ;
241
-
241
+ var dialogHost = ( DialogHost ) dependencyObject ;
242
+
242
243
if ( dialogHost . _popupContentControl != null )
243
244
ValidationAssist . SetSuppress ( dialogHost . _popupContentControl , ! dialogHost . IsOpen ) ;
244
245
VisualStateManager . GoToState ( dialogHost , dialogHost . SelectState ( ) , ! TransitionAssist . GetDisableTransitions ( dialogHost ) ) ;
@@ -260,12 +261,15 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
260
261
dialogHost . _session . IsEnded = true ;
261
262
dialogHost . _session = null ;
262
263
dialogHost . _closeCleanUp ( ) ;
263
-
264
+ dialogHost . Dispatcher . InvokeAsync ( ( ) => dialogHost . _restoreFocusDialogClose . Focus ( ) , DispatcherPriority . Input ) ;
265
+
264
266
return ;
265
267
}
266
268
267
269
dialogHost . _asyncShowWaitHandle . Reset ( ) ;
268
270
dialogHost . _session = new DialogSession ( dialogHost ) ;
271
+ var window = Window . GetWindow ( dialogHost ) ;
272
+ dialogHost . _restoreFocusDialogClose = window != null ? FocusManager . GetFocusedElement ( window ) : null ;
269
273
270
274
//multiple ways of calling back that the dialog has opened:
271
275
// * routed event
@@ -507,7 +511,7 @@ public static DialogClosingEventHandler GetDialogClosingAttached(DependencyObjec
507
511
}
508
512
509
513
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 ) ) ) ;
511
515
512
516
/// <summary>
513
517
/// 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)
678
682
679
683
private void WindowOnDeactivated ( object sender , EventArgs eventArgs )
680
684
{
681
- _restoreFocus = _popup != null ? FocusManager . GetFocusedElement ( ( Window ) sender ) : null ;
685
+ _restoreFocusWindowReactivation = _popup != null ? FocusManager . GetFocusedElement ( ( Window ) sender ) : null ;
682
686
}
683
687
684
688
private void WindowOnActivated ( object sender , EventArgs eventArgs )
685
689
{
686
- if ( _restoreFocus != null )
690
+ if ( _restoreFocusWindowReactivation != null )
687
691
{
688
692
Dispatcher . BeginInvoke ( new Action ( ( ) =>
689
693
{
690
- Keyboard . Focus ( _restoreFocus ) ;
694
+ Keyboard . Focus ( _restoreFocusWindowReactivation ) ;
691
695
} ) ) ;
692
696
}
693
697
}
0 commit comments