From 11ec9e3ede06b74d16ed47377765ce2f9cd0c186 Mon Sep 17 00:00:00 2001 From: Coolkeke <37786276+Coolkeke@users.noreply.github.com> Date: Sat, 16 Dec 2023 04:52:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E6=A1=86BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决控件在VM绑定是照成的newTags和oldTags空值以及下标越界问题 --- src/Ursa/Controls/TagInput/TagInput.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Ursa/Controls/TagInput/TagInput.cs b/src/Ursa/Controls/TagInput/TagInput.cs index f0f0c69..c888fb5 100644 --- a/src/Ursa/Controls/TagInput/TagInput.cs +++ b/src/Ursa/Controls/TagInput/TagInput.cs @@ -143,10 +143,12 @@ public class TagInput : TemplatedControl { Items.RemoveAt(Items.Count - 1); } - - for (int i = 0; i < newTags.Count; i++) + if (newTags != null) { - Items.Insert(Items.Count - 1, newTags[i]); + for (int i = 0; i < newTags.Count; i++) + { + Items.Add(newTags[i]); + } } if (oldTags is INotifyCollectionChanged inccold) From c5f7b9bf5558b5fe608aae637aca07065ae1196d Mon Sep 17 00:00:00 2001 From: Coolkeke <37786276+Coolkeke@users.noreply.github.com> Date: Mon, 18 Dec 2023 20:43:27 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8DTagInputBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调整输入框添加的位置,存放在构造器中 --- src/Ursa/Controls/TagInput/TagInput.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Ursa/Controls/TagInput/TagInput.cs b/src/Ursa/Controls/TagInput/TagInput.cs index c888fb5..488fe3d 100644 --- a/src/Ursa/Controls/TagInput/TagInput.cs +++ b/src/Ursa/Controls/TagInput/TagInput.cs @@ -49,8 +49,11 @@ public class TagInput : TemplatedControl public TagInput() { _textBox = new TextBox(); - _textBox.AddHandler(InputElement.KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel); - Items = new AvaloniaList(); + _textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel); + Items = new AvaloniaList + { + _textBox + }; Tags = new ObservableCollection(); } @@ -123,7 +126,6 @@ public class TagInput : TemplatedControl { base.OnApplyTemplate(e); _itemsControl = e.NameScope.Find(PART_ItemsControl); - Items.Add(_textBox); } private void OnInputThemePropertyChanged(AvaloniaPropertyChangedEventArgs args) @@ -147,10 +149,9 @@ public class TagInput : TemplatedControl { for (int i = 0; i < newTags.Count; i++) { - Items.Add(newTags[i]); + Items.Insert(Items.Count - 1, newTags[i]); } - } - + } if (oldTags is INotifyCollectionChanged inccold) { inccold.CollectionChanged-= OnCollectionChanged; 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 3/3] =?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) {