Pārlūkot izejas kodu

Focus first child from no focus.

If there is no currently focused element when tab navigation occurs, move the focus from the root.

Fixes #11290.
Steven Kirk 2 gadi atpakaļ
vecāks
revīzija
94b9c36e31

+ 8 - 6
src/Avalonia.Base/Input/KeyboardNavigationHandler.cs

@@ -77,13 +77,16 @@ namespace Avalonia.Input
         /// <param name="direction">The direction to move.</param>
         /// <param name="keyModifiers">Any key modifiers active at the time of focus.</param>
         public void Move(
-            IInputElement element,
+            IInputElement? element,
             NavigationDirection direction,
             KeyModifiers keyModifiers = KeyModifiers.None)
         {
-            element = element ?? throw new ArgumentNullException(nameof(element));
+            if (element is null && _owner is null)
+            {
+                return;
+            }
 
-            var next = GetNext(element, direction);
+            var next = GetNext(element ?? _owner!, direction);
 
             if (next != null)
             {
@@ -101,10 +104,9 @@ namespace Avalonia.Input
         /// <param name="e">The event args.</param>
         protected virtual void OnKeyDown(object? sender, KeyEventArgs e)
         {
-            var current = FocusManager.GetFocusManager(e.Source as IInputElement)?.GetFocusedElement();
-
-            if (current != null && e.Key == Key.Tab)
+            if (e.Key == Key.Tab)
             {
+                var current = FocusManager.GetFocusManager(e.Source as IInputElement)?.GetFocusedElement();
                 var direction = (e.KeyModifiers & KeyModifiers.Shift) == 0 ?
                     NavigationDirection.Next : NavigationDirection.Previous;
                 Move(current, direction, e.KeyModifiers);