forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoFocusBehaviour.cs
More file actions
42 lines (37 loc) · 1.21 KB
/
Copy pathAutoFocusBehaviour.cs
File metadata and controls
42 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
namespace SourceGit.Views
{
public class AutoFocusBehaviour : AvaloniaObject
{
public static readonly AttachedProperty<bool> IsEnabledProperty =
AvaloniaProperty.RegisterAttached<AutoFocusBehaviour, TextBox, bool>("IsEnabled");
static AutoFocusBehaviour()
{
IsEnabledProperty.Changed.AddClassHandler<TextBox>(OnIsEnabledChanged);
}
public static bool GetIsEnabled(AvaloniaObject elem)
{
return elem.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(AvaloniaObject elem, bool value)
{
elem.SetValue(IsEnabledProperty, value);
}
private static void OnIsEnabledChanged(TextBox elem, AvaloniaPropertyChangedEventArgs e)
{
if (GetIsEnabled(elem))
{
elem.AttachedToVisualTree += (o, _) =>
{
if (o is TextBox box)
{
box.Focus(NavigationMethod.Directional);
box.CaretIndex = box.Text?.Length ?? 0;
}
};
}
}
}
}