Merge pull request #226 from yangjieshao/main
TagInput 增加 LostFocusBehavior 用于失去焦点后清空TextBox数据
This commit is contained in:
@@ -21,6 +21,7 @@
|
|||||||
<u:TagInput
|
<u:TagInput
|
||||||
Margin="20"
|
Margin="20"
|
||||||
AllowDuplicates="False"
|
AllowDuplicates="False"
|
||||||
|
LostFocusBehavior="Clear"
|
||||||
Separator="-"
|
Separator="-"
|
||||||
Tags="{Binding DistinctTags}" />
|
Tags="{Binding DistinctTags}" />
|
||||||
<ListBox ItemsSource="{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 = new TextBox();
|
||||||
_textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel);
|
_textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel);
|
||||||
|
_textBox.AddHandler(LostFocusEvent, OnTextBox_LostFocus, RoutingStrategies.Bubble);
|
||||||
Items = new AvaloniaList<object>
|
Items = new AvaloniaList<object>
|
||||||
{
|
{
|
||||||
_textBox
|
_textBox
|
||||||
@@ -57,6 +58,19 @@ public class TagInput : TemplatedControl
|
|||||||
Tags = new ObservableCollection<string>();
|
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 =
|
public static readonly StyledProperty<ControlTheme> InputThemeProperty =
|
||||||
AvaloniaProperty.Register<TagInput, ControlTheme>(
|
AvaloniaProperty.Register<TagInput, ControlTheme>(
|
||||||
nameof(InputTheme));
|
nameof(InputTheme));
|
||||||
@@ -86,6 +100,16 @@ public class TagInput : TemplatedControl
|
|||||||
set => SetValue(SeparatorProperty, value);
|
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>(
|
public static readonly StyledProperty<bool> AllowDuplicatesProperty = AvaloniaProperty.Register<TagInput, bool>(
|
||||||
nameof(AllowDuplicates), defaultValue: true);
|
nameof(AllowDuplicates), defaultValue: true);
|
||||||
|
|
||||||
@@ -203,31 +227,7 @@ public class TagInput : TemplatedControl
|
|||||||
{
|
{
|
||||||
if (args.Key == Key.Enter)
|
if (args.Key == Key.Enter)
|
||||||
{
|
{
|
||||||
if (_textBox.Text?.Length > 0)
|
AddTags();
|
||||||
{
|
|
||||||
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 = "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (args.Key == Key.Delete || args.Key == Key.Back)
|
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)
|
public void Close(object o)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user