diff --git a/src/Views/AutoFocusBehaviour.cs b/src/Views/AutoFocusBehaviour.cs index d7423a49..9437abc9 100644 --- a/src/Views/AutoFocusBehaviour.cs +++ b/src/Views/AutoFocusBehaviour.cs @@ -1,4 +1,5 @@ using Avalonia; +using Avalonia.Controls; using Avalonia.Input; namespace SourceGit.Views @@ -6,17 +7,11 @@ namespace SourceGit.Views public class AutoFocusBehaviour : AvaloniaObject { public static readonly AttachedProperty IsEnabledProperty = - AvaloniaProperty.RegisterAttached("IsEnabled", false, false); + AvaloniaProperty.RegisterAttached("IsEnabled", false, false); static AutoFocusBehaviour() { - IsEnabledProperty.Changed.AddClassHandler((input, e) => - { - if (input.GetValue(IsEnabledProperty)) - { - input.AttachedToVisualTree += (o, _) => (o as InputElement).Focus(NavigationMethod.Directional); - } - }); + IsEnabledProperty.Changed.AddClassHandler(OnIsEnabledChanged); } public static bool GetIsEnabled(AvaloniaObject elem) @@ -28,5 +23,18 @@ namespace SourceGit.Views { elem.SetValue(IsEnabledProperty, value); } + + private static void OnIsEnabledChanged(TextBox elem, AvaloniaPropertyChangedEventArgs e) + { + if (GetIsEnabled(elem)) + { + elem.AttachedToVisualTree += (o, _) => + { + var text = o as TextBox; + text.Focus(NavigationMethod.Directional); + text.CaretIndex = text.Text == null ? 0 : text.Text.Length; + }; + } + } } }