Merge pull request #226 from yangjieshao/main
TagInput 增加 LostFocusBehavior 用于失去焦点后清空TextBox数据
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
<u:TagInput
|
||||
Margin="20"
|
||||
AllowDuplicates="False"
|
||||
LostFocusBehavior="Clear"
|
||||
Separator="-"
|
||||
Tags="{Binding DistinctTags}" />
|
||||
<ListBox ItemsSource="{Binding DistinctTags}" />
|
||||
|
||||
8
src/Ursa/Controls/TagInput/LostFocusBehavior.cs
Normal file
8
src/Ursa/Controls/TagInput/LostFocusBehavior.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public enum LostFocusBehavior
|
||||
{
|
||||
None,
|
||||
Add,
|
||||
Clear,
|
||||
}
|
||||
@@ -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,19 @@ public class TagInput : TemplatedControl
|
||||
Tags = new ObservableCollection<string>();
|
||||
}
|
||||
|
||||
private void OnTextBox_LostFocus(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
switch (LostFocusBehavior)
|
||||
{
|
||||
case LostFocusBehavior.Add:
|
||||
AddTags();
|
||||
break;
|
||||
case LostFocusBehavior.Clear:
|
||||
_textBox.Text = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<ControlTheme> InputThemeProperty =
|
||||
AvaloniaProperty.Register<TagInput, ControlTheme>(
|
||||
nameof(InputTheme));
|
||||
@@ -86,6 +100,16 @@ public class TagInput : TemplatedControl
|
||||
set => SetValue(SeparatorProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<LostFocusBehavior> LostFocusBehaviorProperty = AvaloniaProperty.Register<TagInput, LostFocusBehavior>(
|
||||
nameof(LostFocusBehavior));
|
||||
|
||||
public LostFocusBehavior LostFocusBehavior
|
||||
{
|
||||
get => GetValue(LostFocusBehaviorProperty);
|
||||
set => SetValue(LostFocusBehaviorProperty, value);
|
||||
}
|
||||
|
||||
|
||||
public static readonly StyledProperty<bool> AllowDuplicatesProperty = AvaloniaProperty.Register<TagInput, bool>(
|
||||
nameof(AllowDuplicates), defaultValue: true);
|
||||
|
||||
@@ -203,31 +227,7 @@ public class TagInput : TemplatedControl
|
||||
{
|
||||
if (args.Key == Key.Enter)
|
||||
{
|
||||
if (_textBox.Text?.Length > 0)
|
||||
{
|
||||
string[] values;
|
||||
if (!string.IsNullOrEmpty(Separator))
|
||||
{
|
||||
values = _textBox.Text.Split(new string[] { Separator },
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
else
|
||||
{
|
||||
values = new[] { _textBox.Text };
|
||||
}
|
||||
|
||||
if (!AllowDuplicates && Tags != null)
|
||||
values = values.Distinct().Except(Tags).ToArray();
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
int index = Items.Count - 1;
|
||||
// Items.Insert(index, values[i]);
|
||||
Tags?.Insert(index, values[i]);
|
||||
}
|
||||
|
||||
_textBox.Text = "";
|
||||
}
|
||||
AddTags();
|
||||
}
|
||||
else if (args.Key == Key.Delete || args.Key == Key.Back)
|
||||
{
|
||||
@@ -243,6 +243,33 @@ public class TagInput : TemplatedControl
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddTags()
|
||||
{
|
||||
if (!(_textBox.Text?.Length > 0)) return;
|
||||
string[] values;
|
||||
if (!string.IsNullOrEmpty(Separator))
|
||||
{
|
||||
values = _textBox.Text.Split(new string[] { Separator },
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
else
|
||||
{
|
||||
values = new[] { _textBox.Text };
|
||||
}
|
||||
|
||||
if (!AllowDuplicates && Tags != null)
|
||||
values = values.Distinct().Except(Tags).ToArray();
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
int index = Items.Count - 1;
|
||||
// Items.Insert(index, values[i]);
|
||||
Tags?.Insert(index, values[i]);
|
||||
}
|
||||
|
||||
_textBox.Text = "";
|
||||
}
|
||||
|
||||
public void Close(object o)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user