diff --git a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs index a6cf363492..6790a2fc41 100644 --- a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs +++ b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs @@ -826,12 +826,41 @@ namespace Avalonia.Controls } else { - SetCurrentValue(IsDropDownOpenProperty, false); + // Check if we still have focus in the parent's focus scope + if (GetFocusScope() is { } scope && + (FocusManager.GetFocusManager(this)?.GetFocusedElement(scope) is not { } focused || + (focused != this && + (focused is Visual v && !this.IsVisualAncestorOf(v))))) + { + SetCurrentValue(IsDropDownOpenProperty, false); + } + _userCalledPopulate = false; ClearTextBoxSelection(); } _isFocused = hasFocus; + + IFocusScope? GetFocusScope() + { + IInputElement? c = this; + + while (c != null) + { + if (c is IFocusScope scope && + c is Visual v && + v.VisualRoot is Visual root && + root.IsVisible) + { + return scope; + } + + c = (c as Visual)?.GetVisualParent() ?? + ((c as IHostedVisualTreeRoot)?.Host as IInputElement); + } + + return null; + } } /// diff --git a/src/Avalonia.Controls/ListBoxItem.cs b/src/Avalonia.Controls/ListBoxItem.cs index 15136e5992..c4db5a1f6b 100644 --- a/src/Avalonia.Controls/ListBoxItem.cs +++ b/src/Avalonia.Controls/ListBoxItem.cs @@ -100,7 +100,14 @@ namespace Avalonia.Controls ItemsControl.ItemsControlFromItemContaner(this) is ListBox owner) { if (owner.UpdateSelectionFromPointerEvent(this, e)) + { + // As we only update selection from touch/pen on pointer release, we need to raise + // the pointer event on the owner to trigger a commit. + if (e.Pointer.Type != PointerType.Mouse) + owner.RaiseEvent(e); + e.Handled = true; + } } }