using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; /// /// A behavior that allows to select all text on got focus event. /// public class TextBoxSelectAllOnGotFocusBehavior : StyledElementBehavior { /// protected override void OnAttachedToVisualTree() { AssociatedObject?.AddHandler(InputElement.GotFocusEvent, AssociatedObject_GotFocus, RoutingStrategies.Bubble); } /// protected override void OnDetachedFromVisualTree() { AssociatedObject?.RemoveHandler(InputElement.GotFocusEvent, AssociatedObject_GotFocus); } private void AssociatedObject_GotFocus(object? sender, GotFocusEventArgs e) { AssociatedObject?.SelectAll(); } }