From 1d6f3b10ce4bcdac7c86954e2c8185f6f9137f8f Mon Sep 17 00:00:00 2001 From: Coolkeke <37786276+Coolkeke@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:03:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DTagIpoutBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复TextBox值为Null无法删除问题以及AllowDuplicates为false状态 VM中的Tags为null时的异常状态问题 --- demo/Ursa.Demo/Pages/TagInputDemo.axaml | 4 +++- demo/Ursa.Demo/ViewModels/TagInputDemoViewModel.cs | 9 ++++++++- src/Ursa/Controls/TagInput/TagInput.cs | 10 ++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/demo/Ursa.Demo/Pages/TagInputDemo.axaml b/demo/Ursa.Demo/Pages/TagInputDemo.axaml index 8a4f839..822f316 100644 --- a/demo/Ursa.Demo/Pages/TagInputDemo.axaml +++ b/demo/Ursa.Demo/Pages/TagInputDemo.axaml @@ -12,7 +12,9 @@ + AllowDuplicates="False" + Separator="-" + Tags="{Binding Tags}" /> _Tags ; + public ObservableCollection Tags + { + get { return _Tags; } + set { SetProperty(ref _Tags, value); } + } } \ No newline at end of file diff --git a/src/Ursa/Controls/TagInput/TagInput.cs b/src/Ursa/Controls/TagInput/TagInput.cs index 488fe3d..f57e7eb 100644 --- a/src/Ursa/Controls/TagInput/TagInput.cs +++ b/src/Ursa/Controls/TagInput/TagInput.cs @@ -210,24 +210,22 @@ public class TagInput : TemplatedControl values = new[] { _textBox.Text }; } - if (!AllowDuplicates) - { + 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]); + Tags?.Insert(index, values[i]); } _textBox.Text = ""; } } 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) {