修复TagIpoutBUG

修复TextBox值为Null无法删除问题以及AllowDuplicates为false状态 VM中的Tags为null时的异常状态问题
This commit is contained in:
Coolkeke
2023-12-18 21:03:08 +08:00
parent c5f7b9bf55
commit 1d6f3b10ce
3 changed files with 15 additions and 8 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

@@ -210,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)
{ {