From d26d9842566601065ae6c75d982a4ecafb76494a Mon Sep 17 00:00:00 2001 From: Andrey Kunchev Date: Tue, 20 Nov 2018 00:58:23 +0200 Subject: [PATCH] focus dropdown item in more safe manner --- src/Avalonia.Controls/DropDown.cs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Controls/DropDown.cs b/src/Avalonia.Controls/DropDown.cs index 847db8a089..4ac2b6f822 100644 --- a/src/Avalonia.Controls/DropDown.cs +++ b/src/Avalonia.Controls/DropDown.cs @@ -170,6 +170,7 @@ namespace Avalonia.Controls e.Handled = true; } } + base.OnPointerPressed(e); } @@ -199,18 +200,26 @@ namespace Avalonia.Controls private void PopupOpened(object sender, EventArgs e) { - var selectedIndex = SelectedIndex; - - if (selectedIndex != -1) - { - var container = ItemContainerGenerator.ContainerFromIndex(selectedIndex); - container?.Focus(); - } + TryFocusSelectedItem(); } private void SelectedItemChanged(AvaloniaPropertyChangedEventArgs e) { UpdateSelectionBoxItem(e.NewValue); + TryFocusSelectedItem(); + } + + private void TryFocusSelectedItem() + { + var selectedIndex = SelectedIndex; + if (IsDropDownOpen && selectedIndex != -1) + { + var container = ItemContainerGenerator.ContainerFromIndex(selectedIndex); + if (container != null && container.Focusable) + { + container.Focus(); + } + } } private void UpdateSelectionBoxItem(object item)