TagInput 增加 CleanLostFocus 用于失去焦点后清空TextBox数据

This commit is contained in:
杨劼
2024-04-23 22:53:26 +08:00
parent f4ca383f6c
commit 17295c2ddf
2 changed files with 20 additions and 0 deletions

View File

@@ -21,6 +21,7 @@
<u:TagInput
Margin="20"
AllowDuplicates="False"
CleanLostFocus="true"
Separator="-"
Tags="{Binding DistinctTags}" />
<ListBox ItemsSource="{Binding DistinctTags}" />

View File

@@ -50,6 +50,7 @@ public class TagInput : TemplatedControl
{
_textBox = new TextBox();
_textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel);
_textBox.AddHandler(LostFocusEvent, OnTextBox_LostFocus, RoutingStrategies.Bubble);
Items = new AvaloniaList<object>
{
_textBox
@@ -57,6 +58,14 @@ public class TagInput : TemplatedControl
Tags = new ObservableCollection<string>();
}
private void OnTextBox_LostFocus(object? sender, RoutedEventArgs e)
{
if(CleanLostFocus)
{
_textBox.Text = "";
}
}
public static readonly StyledProperty<ControlTheme> InputThemeProperty =
AvaloniaProperty.Register<TagInput, ControlTheme>(
nameof(InputTheme));
@@ -86,6 +95,16 @@ public class TagInput : TemplatedControl
set => SetValue(SeparatorProperty, value);
}
public static readonly StyledProperty<bool> CleanLostFocusProperty = AvaloniaProperty.Register<TagInput, bool>(
nameof(CleanLostFocus), defaultValue: false);
public bool CleanLostFocus
{
get => GetValue(CleanLostFocusProperty);
set => SetValue(CleanLostFocusProperty, value);
}
public static readonly StyledProperty<bool> AllowDuplicatesProperty = AvaloniaProperty.Register<TagInput, bool>(
nameof(AllowDuplicates), defaultValue: true);