using Avalonia.Controls; using Avalonia.Input; using Avalonia.Threading; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; /// /// Sets property to true of the associated control on event. /// public class SelectListBoxItemOnPointerMovedBehavior : StyledElementBehavior { /// protected override void OnAttachedToVisualTree() { if (AssociatedObject is not null) { AssociatedObject.PointerMoved += PointerMoved; } } /// protected override void OnDetachedFromVisualTree() { if (AssociatedObject is not null) { AssociatedObject.PointerMoved -= PointerMoved; } } private void PointerMoved(object? sender, PointerEventArgs args) { if (AssociatedObject is {Parent: ListBoxItem item}) { item.SetCurrentValue(ListBoxItem.IsSelectedProperty, true); Dispatcher.UIThread.Post(() => item.Focus()); } } }