From 10c2ec64add55f7bceeec68d6140569a781f86ab Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 22 Jul 2018 22:53:40 +0200 Subject: [PATCH] Implement directional navigation in ItemsControl. --- src/Avalonia.Controls/ItemsControl.cs | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Avalonia.Controls/ItemsControl.cs b/src/Avalonia.Controls/ItemsControl.cs index 3cb997f615..998111e8b9 100644 --- a/src/Avalonia.Controls/ItemsControl.cs +++ b/src/Avalonia.Controls/ItemsControl.cs @@ -323,6 +323,41 @@ namespace Avalonia.Controls LogicalChildren.RemoveAll(toRemove); } + protected override void OnKeyDown(KeyEventArgs e) + { + if (Presenter?.Panel is INavigableContainer container) + { + var focus = FocusManager.Instance; + var current = focus.Current; + NavigationDirection? direction = null; + + switch (e.Key) + { + case Key.Up: direction = NavigationDirection.Up; break; + case Key.Down: direction = NavigationDirection.Down; break; + case Key.Left: direction = NavigationDirection.Left; break; + case Key.Right: direction = NavigationDirection.Right; break; + case Key.Home: direction = NavigationDirection.First; break; + case Key.End: direction = NavigationDirection.Last; break; + case Key.PageUp: direction = NavigationDirection.PageUp; break; + case Key.PageDown: direction = NavigationDirection.PageDown; break; + } + + if (direction != null && current != null) + { + var next = container.GetControl(direction.Value, current); + + if (next != null) + { + focus.Focus(next, NavigationMethod.Directional); + e.Handled = true; + } + } + } + + base.OnKeyDown(e); + } + /// /// Caled when the property changes. ///