feat: support AcceptReturn.

This commit is contained in:
rabbitism
2024-06-23 17:39:11 +08:00
parent 91633402fb
commit 63c0b0547f
3 changed files with 39 additions and 7 deletions

View File

@@ -26,5 +26,13 @@
Separator="-" Separator="-"
Tags="{Binding DistinctTags}" /> Tags="{Binding DistinctTags}" />
<ListBox ItemsSource="{Binding DistinctTags}" /> <ListBox ItemsSource="{Binding DistinctTags}" />
<u:TagInput
Margin="20"
AllowDuplicates="False"
AcceptsReturn="True"
LostFocusBehavior="Clear"
Separator="-"
Tags="{Binding DistinctTags}" />
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -29,6 +29,7 @@
Name="{x:Static u:TagInput.PART_Watermark}" Name="{x:Static u:TagInput.PART_Watermark}"
Opacity="0.5" Opacity="0.5"
IsVisible="False" IsVisible="False"
VerticalAlignment="Center"
Text="{TemplateBinding Watermark}" /> Text="{TemplateBinding Watermark}" />
<ItemsControl <ItemsControl
Name="PART_ItemsControl" Name="PART_ItemsControl"

View File

@@ -30,6 +30,16 @@ public class TagInput : TemplatedControl
public static readonly StyledProperty<string?> WatermarkProperty = TextBox.WatermarkProperty.AddOwner<TagInput>(); public static readonly StyledProperty<string?> WatermarkProperty = TextBox.WatermarkProperty.AddOwner<TagInput>();
public static readonly StyledProperty<bool> AcceptsReturnProperty =
TextBox.AcceptsReturnProperty.AddOwner<TagInput>();
public bool AcceptsReturn
{
get => GetValue(AcceptsReturnProperty);
set => SetValue(AcceptsReturnProperty, value);
}
public static readonly StyledProperty<int> MaxCountProperty = AvaloniaProperty.Register<TagInput, int>( public static readonly StyledProperty<int> MaxCountProperty = AvaloniaProperty.Register<TagInput, int>(
nameof(MaxCount), int.MaxValue); nameof(MaxCount), int.MaxValue);
@@ -82,6 +92,7 @@ public class TagInput : TemplatedControl
public TagInput() public TagInput()
{ {
_textBox = new TextBox(); _textBox = new TextBox();
_textBox[!AcceptsReturnProperty] = this.GetObservable(AcceptsReturnProperty).ToBinding();
_textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel); _textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel);
_textBox.AddHandler(LostFocusEvent, OnTextBox_LostFocus, RoutingStrategies.Bubble); _textBox.AddHandler(LostFocusEvent, OnTextBox_LostFocus, RoutingStrategies.Bubble);
Items = new AvaloniaList<object> Items = new AvaloniaList<object>
@@ -162,7 +173,7 @@ public class TagInput : TemplatedControl
switch (LostFocusBehavior) switch (LostFocusBehavior)
{ {
case LostFocusBehavior.Add: case LostFocusBehavior.Add:
AddTags(); AddTags(_textBox.Text);
break; break;
case LostFocusBehavior.Clear: case LostFocusBehavior.Clear:
_textBox.Text = ""; _textBox.Text = "";
@@ -257,8 +268,20 @@ public class TagInput : TemplatedControl
private void OnTextBoxKeyDown(object? sender, KeyEventArgs args) private void OnTextBoxKeyDown(object? sender, KeyEventArgs args)
{ {
if (args.Key == Key.Enter) if (!AcceptsReturn && args.Key == Key.Enter)
AddTags(); {
AddTags(_textBox.Text);
}
else if (AcceptsReturn && args.Key==Key.Enter)
{
var texts = _textBox.Text?.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries) ??
[];
foreach (var text in texts)
{
AddTags(text);
}
args.Handled = true;
}
else if (args.Key == Key.Delete || args.Key == Key.Back) else if (args.Key == Key.Delete || args.Key == Key.Back)
if (string.IsNullOrEmpty(_textBox.Text) || _textBox.Text?.Length == 0) if (string.IsNullOrEmpty(_textBox.Text) || _textBox.Text?.Length == 0)
{ {
@@ -269,13 +292,13 @@ public class TagInput : TemplatedControl
} }
} }
private void AddTags() private void AddTags(string? text)
{ {
if (!(_textBox.Text?.Length > 0)) return; if (!(text?.Length > 0)) return;
if (Tags.Count >= MaxCount) return; if (Tags.Count >= MaxCount) return;
string[] values; string[] values;
if (!string.IsNullOrEmpty(Separator)) if (!string.IsNullOrEmpty(Separator))
values = _textBox.Text.Split(new[] { Separator }, values = text.Split(new[] { Separator },
StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries);
else else
values = new[] { _textBox.Text }; values = new[] { _textBox.Text };
@@ -290,7 +313,7 @@ public class TagInput : TemplatedControl
Tags?.Insert(index, values[i]); Tags?.Insert(index, values[i]);
} }
_textBox.Text = ""; _textBox.Clear();
} }
public void Close(object o) public void Close(object o)