@@ -65,6 +65,8 @@ public class DialogHost : ContentControl
65
65
private DialogOpenedEventHandler _attachedDialogOpenedEventHandler ;
66
66
private DialogClosingEventHandler _attachedDialogClosingEventHandler ;
67
67
private object _closeDialogExecutionParameter ;
68
+ private IInputElement _restoreFocus ;
69
+ private Action _closeCleanUp = ( ) => { } ;
68
70
69
71
static DialogHost ( )
70
72
{
@@ -204,7 +206,7 @@ public DialogHost()
204
206
Unloaded += OnUnloaded ;
205
207
206
208
CommandBindings . Add ( new CommandBinding ( CloseDialogCommand , CloseDialogHandler , CloseDialogCanExecute ) ) ;
207
- CommandBindings . Add ( new CommandBinding ( OpenDialogCommand , OpenDialogHandler ) ) ;
209
+ CommandBindings . Add ( new CommandBinding ( OpenDialogCommand , OpenDialogHandler ) ) ;
208
210
}
209
211
210
212
public static readonly DependencyProperty IdentifierProperty = DependencyProperty . Register (
@@ -220,7 +222,7 @@ public object Identifier
220
222
}
221
223
222
224
public static readonly DependencyProperty IsOpenProperty = DependencyProperty . Register (
223
- "IsOpen" , typeof ( bool ) , typeof ( DialogHost ) , new FrameworkPropertyMetadata ( default ( bool ) , FrameworkPropertyMetadataOptions . BindsTwoWayByDefault , IsOpenPropertyChangedCallback ) ) ;
225
+ "IsOpen" , typeof ( bool ) , typeof ( DialogHost ) , new FrameworkPropertyMetadata ( default ( bool ) , FrameworkPropertyMetadataOptions . BindsTwoWayByDefault , IsOpenPropertyChangedCallback ) ) ;
224
226
225
227
private static void IsOpenPropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
226
228
{
@@ -229,12 +231,18 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
229
231
ValidationAssist . SetSuppress ( dialogHost . _popupContentControl , ! dialogHost . IsOpen ) ;
230
232
VisualStateManager . GoToState ( dialogHost , dialogHost . SelectState ( ) , ! TransitionAssist . GetDisableTransitions ( dialogHost ) ) ;
231
233
232
- if ( ! dialogHost . IsOpen )
234
+
235
+ if ( dialogHost . IsOpen )
236
+ {
237
+ WatchWindowActivation ( dialogHost ) ;
238
+ }
239
+ else
233
240
{
234
241
dialogHost . _asyncShowWaitHandle . Set ( ) ;
235
242
dialogHost . _attachedDialogClosingEventHandler = null ;
236
243
dialogHost . _session . IsEnded = true ;
237
244
dialogHost . _session = null ;
245
+ dialogHost . _closeCleanUp ( ) ;
238
246
239
247
return ;
240
248
}
@@ -265,7 +273,7 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
265
273
//totally not happy about this, but on immediate validation we can get some wierd looking stuff...give WPF a kick to refresh...
266
274
Task . Delay ( 300 ) . ContinueWith ( t => child . Dispatcher . BeginInvoke ( new Action ( ( ) => child . InvalidateVisual ( ) ) ) ) ;
267
275
} ) ) ;
268
- }
276
+ }
269
277
270
278
public bool IsOpen
271
279
{
@@ -470,6 +478,14 @@ internal void Close(object parameter)
470
478
_closeDialogExecutionParameter = parameter ;
471
479
}
472
480
481
+ protected override void OnPreviewMouseDown ( MouseButtonEventArgs e )
482
+ {
483
+ var window = Window . GetWindow ( this ) ;
484
+ if ( window != null && ! window . IsActive )
485
+ window . Activate ( ) ;
486
+ base . OnPreviewMouseDown ( e ) ;
487
+ }
488
+
473
489
private void OpenDialogHandler ( object sender , ExecutedRoutedEventArgs executedRoutedEventArgs )
474
490
{
475
491
if ( executedRoutedEventArgs . Handled ) return ;
@@ -541,5 +557,38 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
541
557
{
542
558
LoadedInstances . Add ( this ) ;
543
559
}
560
+
561
+ private static void WatchWindowActivation ( DialogHost dialogHost )
562
+ {
563
+ var window = Window . GetWindow ( dialogHost ) ;
564
+ if ( window != null )
565
+ {
566
+ window . Activated += dialogHost . WindowOnActivated ;
567
+ window . Deactivated += dialogHost . WindowOnDeactivated ;
568
+ dialogHost . _closeCleanUp = ( ) =>
569
+ {
570
+ window . Activated -= dialogHost . WindowOnActivated ;
571
+ window . Deactivated -= dialogHost . WindowOnDeactivated ;
572
+ } ;
573
+ }
574
+ else
575
+ {
576
+ dialogHost . _closeCleanUp = ( ) => { } ;
577
+ }
578
+ }
579
+
580
+ private void WindowOnDeactivated ( object sender , EventArgs eventArgs )
581
+ {
582
+ _restoreFocus = _popupContentControl != null ? FocusManager . GetFocusedElement ( _popupContentControl ) : null ;
583
+ }
584
+
585
+ private void WindowOnActivated ( object sender , EventArgs eventArgs )
586
+ {
587
+ if ( _restoreFocus != null )
588
+ {
589
+ Dispatcher . BeginInvoke ( new Action ( ( ) => Keyboard . Focus ( _restoreFocus ) ) ) ;
590
+
591
+ }
592
+ }
544
593
}
545
594
}
0 commit comments