File tree Expand file tree Collapse file tree 1 file changed +28
-5
lines changed Expand file tree Collapse file tree 1 file changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -816,14 +816,37 @@ internal void InternalClose(object? parameter)
816
816
if ( child is null ) return null ;
817
817
818
818
CommandManager . InvalidateRequerySuggested ( ) ;
819
- var focusable = child . VisualDepthFirstTraversal ( ) . OfType < UIElement > ( ) . FirstOrDefault ( ui => ui . Focusable && ui . IsVisible ) ;
820
- focusable ? . Dispatcher . InvokeAsync ( ( ) =>
819
+ var focusable = child . VisualDepthFirstTraversal ( ) . OfType < UIElement > ( ) . FirstOrDefault ( ui => ui . Focusable ) ;
820
+ if ( focusable is null ) return null ;
821
+
822
+ if ( focusable . IsVisible )
823
+ {
824
+ MoveFocus ( focusable ) ;
825
+ }
826
+ else
821
827
{
822
- if ( ! focusable . Focus ( ) ) return ;
823
- focusable . MoveFocus ( new TraversalRequest ( FocusNavigationDirection . First ) ) ;
824
- } , DispatcherPriority . Background ) ;
828
+ focusable . IsVisibleChanged += FocusableOnIsVisibleChanged ;
829
+ }
825
830
826
831
return child ;
832
+
833
+ void FocusableOnIsVisibleChanged ( object sender , DependencyPropertyChangedEventArgs e )
834
+ {
835
+ if ( sender is UIElement focusable )
836
+ {
837
+ MoveFocus ( focusable ) ;
838
+ focusable . IsVisibleChanged -= FocusableOnIsVisibleChanged ;
839
+ }
840
+ }
841
+
842
+ void MoveFocus ( UIElement focusable )
843
+ {
844
+ focusable . Dispatcher . BeginInvoke ( ( ) =>
845
+ {
846
+ if ( ! focusable . Focus ( ) ) return ;
847
+ focusable . MoveFocus ( new TraversalRequest ( FocusNavigationDirection . First ) ) ;
848
+ } , DispatcherPriority . Background ) ;
849
+ }
827
850
}
828
851
829
852
protected override void OnPreviewMouseDown ( MouseButtonEventArgs e )
You can’t perform that action at this time.
0 commit comments