diff --git a/src/Ursa/Controls/TagInput/TagInput.cs b/src/Ursa/Controls/TagInput/TagInput.cs index fb64d1a..32a3e64 100644 --- a/src/Ursa/Controls/TagInput/TagInput.cs +++ b/src/Ursa/Controls/TagInput/TagInput.cs @@ -30,13 +30,14 @@ public class TagInput: TemplatedControl get => GetValue(TagsProperty); set => SetValue(TagsProperty, value); } - - public static readonly StyledProperty ItemsProperty = AvaloniaProperty.Register( - nameof(Items)); + + public static readonly DirectProperty ItemsProperty = AvaloniaProperty.RegisterDirect( + nameof(Items), o => o.Items); + private IList _items; public IList Items { - get => GetValue(ItemsProperty); - set => SetValue(ItemsProperty, value); + get => _items; + private set => SetAndRaise(ItemsProperty, ref _items, value); } public TagInput() @@ -83,17 +84,41 @@ public class TagInput: TemplatedControl set => SetValue(AllowDuplicatesProperty, value); } + static TagInput() + { + InputThemeProperty.Changed.AddClassHandler((o, e) => o.OnInputThemePropertyChanged(e)); + TagsProperty.Changed.AddClassHandler((o, e) => o.OnTagsPropertyChanged(e)); + } + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); _itemsControl = e.NameScope.Find(PART_ItemsControl); - if (IsSet(InputThemeProperty) && InputTheme.TargetType == typeof(TextBox)) - { - _textBox.Theme = InputTheme; - } Items.Add(_textBox); } + private void OnInputThemePropertyChanged(AvaloniaPropertyChangedEventArgs args) + { + var newTheme = args.GetNewValue(); + if (newTheme.TargetType == typeof(TextBox)) + { + _textBox.Theme = newTheme; + } + } + + private void OnTagsPropertyChanged(AvaloniaPropertyChangedEventArgs args) + { + var newTags = args.GetNewValue>(); + for (int i = 0; i < Items.Count - 1; i++) + { + Items.RemoveAt(Items.Count-1); + } + for (int i = 0; i < newTags.Count; i++) + { + Items.Insert(Items.Count - 1, newTags[i]); + } + } + private void OnTextBoxKeyDown(object? sender, KeyEventArgs args) { if (args.Key == Key.Enter) @@ -141,8 +166,7 @@ public class TagInput: TemplatedControl { if (o is Control t) { - var presenter = t.Parent as ContentPresenter; - if (presenter != null) + if (t.Parent is ContentPresenter presenter) { int? index = _itemsControl?.IndexFromContainer(presenter); if (index is >= 0 && index < Items.Count - 1)