Merge pull request #57 from Coolkeke/main

修复标签输入框BUG
This commit is contained in:
Dong Bin
2023-12-26 15:37:28 +08:00
committed by GitHub
3 changed files with 26 additions and 16 deletions

View File

@@ -12,7 +12,9 @@
<u:TagInput <u:TagInput
Name="labels" Name="labels"
Margin="20" Margin="20"
Separator="-" /> AllowDuplicates="False"
Separator="-"
Tags="{Binding Tags}" />
<u:TagInput <u:TagInput
Margin="20" Margin="20"
AllowDuplicates="False" AllowDuplicates="False"

View File

@@ -1,6 +1,13 @@
using System.Collections.ObjectModel;
namespace Ursa.Demo.ViewModels; namespace Ursa.Demo.ViewModels;
public class TagInputDemoViewModel: ViewModelBase public class TagInputDemoViewModel: ViewModelBase
{ {
private ObservableCollection<string> _Tags ;
public ObservableCollection<string> Tags
{
get { return _Tags; }
set { SetProperty(ref _Tags, value); }
}
} }

View File

@@ -49,8 +49,11 @@ public class TagInput : TemplatedControl
public TagInput() public TagInput()
{ {
_textBox = new TextBox(); _textBox = new TextBox();
_textBox.AddHandler(InputElement.KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel); _textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel);
Items = new AvaloniaList<object>(); Items = new AvaloniaList<object>
{
_textBox
};
Tags = new ObservableCollection<string>(); Tags = new ObservableCollection<string>();
} }
@@ -123,7 +126,6 @@ public class TagInput : TemplatedControl
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
_itemsControl = e.NameScope.Find<ItemsControl>(PART_ItemsControl); _itemsControl = e.NameScope.Find<ItemsControl>(PART_ItemsControl);
Items.Add(_textBox);
} }
private void OnInputThemePropertyChanged(AvaloniaPropertyChangedEventArgs args) private void OnInputThemePropertyChanged(AvaloniaPropertyChangedEventArgs args)
@@ -143,12 +145,13 @@ public class TagInput : TemplatedControl
{ {
Items.RemoveAt(Items.Count - 1); Items.RemoveAt(Items.Count - 1);
} }
if (newTags != null)
for (int i = 0; i < newTags.Count; i++)
{ {
Items.Insert(Items.Count - 1, newTags[i]); for (int i = 0; i < newTags.Count; i++)
} {
Items.Insert(Items.Count - 1, newTags[i]);
}
}
if (oldTags is INotifyCollectionChanged inccold) if (oldTags is INotifyCollectionChanged inccold)
{ {
inccold.CollectionChanged-= OnCollectionChanged; inccold.CollectionChanged-= OnCollectionChanged;
@@ -207,24 +210,22 @@ public class TagInput : TemplatedControl
values = new[] { _textBox.Text }; values = new[] { _textBox.Text };
} }
if (!AllowDuplicates) if (!AllowDuplicates && Tags != null)
{
values = values.Distinct().Except(Tags).ToArray(); values = values.Distinct().Except(Tags).ToArray();
}
for (int i = 0; i < values.Length; i++) for (int i = 0; i < values.Length; i++)
{ {
int index = Items.Count - 1; int index = Items.Count - 1;
// Items.Insert(index, values[i]); // Items.Insert(index, values[i]);
Tags.Insert(index, values[i]); Tags?.Insert(index, values[i]);
} }
_textBox.Text = ""; _textBox.Text = "";
} }
} }
else if (args.Key == Key.Delete || args.Key == Key.Back) else if (args.Key == Key.Delete || args.Key == Key.Back)
{ {
if (_textBox.Text?.Length == 0) if (string.IsNullOrEmpty(_textBox.Text)||_textBox.Text?.Length == 0)
{ {
if (Tags.Count == 0) if (Tags.Count == 0)
{ {