Skip to content

Commit ddd8326

Browse files
committed
re-focus popup when updating content #320
1 parent ea11a32 commit ddd8326

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,13 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
266266

267267
dialogHost.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
268268
{
269-
var child = dialogHost._popup?.Child;
270-
if (child == null) return;
271-
272-
child.Focus();
273-
child.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
269+
var child = dialogHost.FocusPopup();
274270

275271
//https://github.com/ButchersBoy/MaterialDesignInXamlToolkit/issues/187
276272
//totally not happy about this, but on immediate validation we can get some weird looking stuff...give WPF a kick to refresh...
277-
Task.Delay(300).ContinueWith(t => child.Dispatcher.BeginInvoke(new Action(() => child.InvalidateVisual())));
273+
Task.Delay(300).ContinueWith(t => child.Dispatcher.BeginInvoke(new Action(() => child.InvalidateVisual())));
278274
}));
279-
}
275+
}
280276

281277
public bool IsOpen
282278
{
@@ -512,6 +508,20 @@ internal void Close(object parameter)
512508
_closeDialogExecutionParameter = parameter;
513509
}
514510

511+
/// <summary>
512+
/// Attempts to focus the content of a popup.
513+
/// </summary>
514+
/// <returns>The popup content.</returns>
515+
internal UIElement FocusPopup()
516+
{
517+
var child = _popup?.Child;
518+
if (child == null) return null;
519+
520+
child.Focus();
521+
child.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
522+
return child;
523+
}
524+
515525
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
516526
{
517527
var window = Window.GetWindow(this);

MaterialDesignThemes.Wpf/DialogSession.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Windows.Threading;
23

34
namespace MaterialDesignThemes.Wpf
45
{
@@ -39,6 +40,7 @@ public void UpdateContent(object content)
3940

4041
_owner.AssertTargetableContent();
4142
_owner.DialogContent = content;
43+
_owner.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => _owner.FocusPopup()));
4244
}
4345

4446
/// <summary>

0 commit comments

Comments
 (0)