diff --git a/src/Avalonia.Input/Navigation/TabNavigation.cs b/src/Avalonia.Input/Navigation/TabNavigation.cs index 6e077e887f..a9d5b83073 100644 --- a/src/Avalonia.Input/Navigation/TabNavigation.cs +++ b/src/Avalonia.Input/Navigation/TabNavigation.cs @@ -221,17 +221,16 @@ namespace Avalonia.Input.Navigation return parent; } - var siblings = parent.GetVisualChildren() + var allSiblings = parent.GetVisualChildren() .OfType() .Where(FocusExtensions.CanFocusDescendants); - var sibling = direction == NavigationDirection.Next ? - siblings.SkipWhile(x => x != container).Skip(1).FirstOrDefault() : - siblings.TakeWhile(x => x != container).LastOrDefault(); + var siblings = direction == NavigationDirection.Next ? + allSiblings.SkipWhile(x => x != container).Skip(1) : + allSiblings.TakeWhile(x => x != container).Reverse(); - if (sibling != null) + foreach (var sibling in siblings) { var customNext = GetCustomNext(sibling, direction); - if (customNext.handled) { return customNext.next; @@ -239,13 +238,17 @@ namespace Avalonia.Input.Navigation if (sibling.CanFocus()) { - next = sibling; + return sibling; } else { next = direction == NavigationDirection.Next ? GetFocusableDescendants(sibling, direction).FirstOrDefault() : GetFocusableDescendants(sibling, direction).LastOrDefault(); + if(next != null) + { + return next; + } } } diff --git a/tests/Avalonia.Input.UnitTests/KeyboardNavigationTests_Tab.cs b/tests/Avalonia.Input.UnitTests/KeyboardNavigationTests_Tab.cs index ad70dcd470..e779652322 100644 --- a/tests/Avalonia.Input.UnitTests/KeyboardNavigationTests_Tab.cs +++ b/tests/Avalonia.Input.UnitTests/KeyboardNavigationTests_Tab.cs @@ -80,6 +80,43 @@ namespace Avalonia.Input.UnitTests Assert.Equal(next, result); } + [Fact] + public void Next_Skips_Unfocusable_Siblings() + { + Button current; + Button next; + + var top = new StackPanel + { + Children = + { + new StackPanel + { + Children = + { + new Button { Name = "Button1" }, + new Button { Name = "Button2" }, + new StackPanel + { + Children = + { + (current = new Button { Name = "Button3" }), + } + }, + new TextBlock { Name = "TextBlock" }, + (next = new Button { Name = "Button4" }), + } + }, + new Button { Name = "Button5" }, + new Button { Name = "Button6" }, + } + }; + + var result = KeyboardNavigationHandler.GetNext(current, NavigationDirection.Next); + + Assert.Equal(next, result); + } + [Fact] public void Next_Continue_Doesnt_Enter_Panel_With_TabNavigation_None() {