From 4f13d504967aa9be3ccd2123189dece3591d32ad Mon Sep 17 00:00:00 2001 From: Jurjen Biewenga Date: Thu, 28 Sep 2017 15:53:05 +0200 Subject: [PATCH 1/3] Added initial changes to allow the user to change the selected item while the dropdown is closed but selected and allows the user to open the dropdown by pressing down --- src/Avalonia.Controls/DropDown.cs | 24 ++++++++++++++++++++++++ src/Avalonia.Controls/ItemsControl.cs | 11 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Avalonia.Controls/DropDown.cs b/src/Avalonia.Controls/DropDown.cs index 5349fb1ca7..63fd6726b4 100644 --- a/src/Avalonia.Controls/DropDown.cs +++ b/src/Avalonia.Controls/DropDown.cs @@ -114,6 +114,30 @@ namespace Avalonia.Controls IsDropDownOpen = false; e.Handled = true; } + + if (!IsDropDownOpen) + { + if (e.Key == Key.Right) + { + if (++SelectedIndex >= ItemCount) + SelectedIndex = 0; + + e.Handled = true; + } + else if (e.Key == Key.Left) + { + if (--SelectedIndex < 0) + SelectedIndex = ItemCount - 1; + e.Handled = true; + } + else if (e.Key == Key.Down) + { + IsDropDownOpen = true; + if (SelectedIndex == -1) + SelectedIndex = 0; + e.Handled = true; + } + } } } diff --git a/src/Avalonia.Controls/ItemsControl.cs b/src/Avalonia.Controls/ItemsControl.cs index aa209e0462..4366de1cd6 100644 --- a/src/Avalonia.Controls/ItemsControl.cs +++ b/src/Avalonia.Controls/ItemsControl.cs @@ -11,6 +11,7 @@ using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; using Avalonia.Controls.Utils; +using Avalonia.Input; using Avalonia.LogicalTree; using Avalonia.Metadata; @@ -106,6 +107,12 @@ namespace Avalonia.Controls set { SetAndRaise(ItemsProperty, ref _items, value); } } + public int ItemCount + { + get; + private set; + } + /// /// Gets or sets the panel used to display the items. /// @@ -352,6 +359,10 @@ namespace Avalonia.Controls RemoveControlItemsFromLogicalChildren(e.OldItems); break; } + + int? count = (Items as IList)?.Count; + if (count != null) + ItemCount = (int)count; var collection = sender as ICollection; PseudoClasses.Set(":empty", collection == null || collection.Count == 0); From 3081a07f1c64403a668049d8e0652255b26fc888 Mon Sep 17 00:00:00 2001 From: Jurjen Biewenga Date: Mon, 16 Oct 2017 11:27:06 +0200 Subject: [PATCH 2/3] Made the requested changes --- src/Avalonia.Controls/DropDown.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Controls/DropDown.cs b/src/Avalonia.Controls/DropDown.cs index c38f9ffd9e..cdb179cac9 100644 --- a/src/Avalonia.Controls/DropDown.cs +++ b/src/Avalonia.Controls/DropDown.cs @@ -104,7 +104,7 @@ namespace Avalonia.Controls if (!e.Handled) { if (e.Key == Key.F4 || - (e.Key == Key.Down && ((e.Modifiers & InputModifiers.Alt) != 0))) + ((e.Key == Key.Down || e.Key == Key.Up) && ((e.Modifiers & InputModifiers.Alt) != 0))) { IsDropDownOpen = !IsDropDownOpen; e.Handled = true; @@ -117,24 +117,21 @@ namespace Avalonia.Controls if (!IsDropDownOpen) { - if (e.Key == Key.Right) + if (e.Key == Key.Down) { + if (SelectedIndex == -1) + SelectedIndex = 0; + if (++SelectedIndex >= ItemCount) SelectedIndex = 0; e.Handled = true; } - else if (e.Key == Key.Left) + else if (e.Key == Key.Up) { if (--SelectedIndex < 0) SelectedIndex = ItemCount - 1; - e.Handled = true; - } - else if (e.Key == Key.Down) - { - IsDropDownOpen = true; - if (SelectedIndex == -1) - SelectedIndex = 0; + e.Handled = true; } } From 2f4887318495bfbf1fffa772bf65e729a678eceb Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 19 Oct 2017 00:26:27 +0200 Subject: [PATCH 3/3] Select dropdown item on focus. Arrow keys within the dropdown are handled by the keyboard navigation handler, so detect when this focuses a new item and select it. --- src/Avalonia.Controls/DropDown.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Avalonia.Controls/DropDown.cs b/src/Avalonia.Controls/DropDown.cs index cdb179cac9..6b27c479ba 100644 --- a/src/Avalonia.Controls/DropDown.cs +++ b/src/Avalonia.Controls/DropDown.cs @@ -96,6 +96,16 @@ namespace Avalonia.Controls this.UpdateSelectionBoxItem(this.SelectedItem); } + protected override void OnGotFocus(GotFocusEventArgs e) + { + base.OnGotFocus(e); + + if (!e.Handled && e.NavigationMethod == NavigationMethod.Directional) + { + e.Handled = UpdateSelectionFromEventSource(e.Source); + } + } + /// protected override void OnKeyDown(KeyEventArgs e) {