Merge pull request #697 from irihitech/autocomplete

Fix AutoCompleteBox focus behavior in Avalonia 11.3
This commit is contained in:
Dong Bin
2025-06-09 20:08:25 +08:00
committed by GitHub

View File

@@ -1,7 +1,4 @@
using System.Diagnostics; using Avalonia.Controls.Templates;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Irihi.Avalonia.Shared.Contracts; using Irihi.Avalonia.Shared.Contracts;
@@ -20,23 +17,24 @@ public class AutoCompleteBox: Avalonia.Controls.AutoCompleteBox, IClearControl
AddHandler(PointerPressedEvent, OnBoxPointerPressed, RoutingStrategies.Tunnel); AddHandler(PointerPressedEvent, OnBoxPointerPressed, RoutingStrategies.Tunnel);
} }
public void Clear()
{
SetCurrentValue(SelectedItemProperty, null);
}
private void OnBoxPointerPressed(object? sender, PointerPressedEventArgs e) private void OnBoxPointerPressed(object? sender, PointerPressedEventArgs e)
{ {
if (Equals(sender, this) && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) if (Equals(sender, this) && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && IsDropDownOpen == false)
{
SetCurrentValue(IsDropDownOpenProperty, true); SetCurrentValue(IsDropDownOpenProperty, true);
} }
}
protected override void OnGotFocus(GotFocusEventArgs e) protected override void OnGotFocus(GotFocusEventArgs e)
{ {
base.OnGotFocus(e); base.OnGotFocus(e);
if (IsDropDownOpen) return; // If the focus is set by pointer navigation, it is handled by PointerPressed, do not open the dropdown.
SetCurrentValue(IsDropDownOpenProperty, true); if (e.NavigationMethod == NavigationMethod.Pointer) return;
} if (!this.GetTemplateChildren().Contains(e.Source)) return;
// If the focus is set by keyboard navigation, open the dropdown.
public void Clear() if (IsDropDownOpen == false) SetCurrentValue(IsDropDownOpenProperty, true);
{
SetCurrentValue(SelectedItemProperty, null);
} }
} }