Skip to content

Commit d238ea0

Browse files
committed
restore focus back to previous element on dialog close
1 parent 8123c06 commit d238ea0

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Button.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<Trigger Property="IsMouseOver" Value="true">
6161
<Setter TargetName="border" Property="wpf:ShadowAssist.Darken" Value="True" />
6262
</Trigger>
63+
<Trigger Property="IsKeyboardFocused" Value="true">
64+
<Setter TargetName="border" Property="wpf:ShadowAssist.Darken" Value="True" />
65+
</Trigger>
6366
<Trigger Property="IsEnabled" Value="false">
6467
<Setter Property="Opacity" Value="0.23"/>
6568
</Trigger>
@@ -205,6 +208,9 @@
205208
<Trigger Property="IsMouseOver" Value="True">
206209
<Setter TargetName="border" Property="wpf:ShadowAssist.Darken" Value="True" />
207210
</Trigger>
211+
<Trigger Property="IsKeyboardFocused" Value="true">
212+
<Setter TargetName="border" Property="wpf:ShadowAssist.Darken" Value="True" />
213+
</Trigger>
208214
</ControlTemplate.Triggers>
209215
</ControlTemplate>
210216
</Setter.Value>

0 commit comments

Comments
 (0)