feat: change Items to readonly.
This commit is contained in:
@@ -31,12 +31,13 @@ public class TagInput: TemplatedControl
|
|||||||
set => SetValue(TagsProperty, value);
|
set => SetValue(TagsProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<IList> ItemsProperty = AvaloniaProperty.Register<TagInput, IList>(
|
public static readonly DirectProperty<TagInput, IList> ItemsProperty = AvaloniaProperty.RegisterDirect<TagInput, IList>(
|
||||||
nameof(Items));
|
nameof(Items), o => o.Items);
|
||||||
|
private IList _items;
|
||||||
public IList Items
|
public IList Items
|
||||||
{
|
{
|
||||||
get => GetValue(ItemsProperty);
|
get => _items;
|
||||||
set => SetValue(ItemsProperty, value);
|
private set => SetAndRaise(ItemsProperty, ref _items, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TagInput()
|
public TagInput()
|
||||||
@@ -83,17 +84,41 @@ public class TagInput: TemplatedControl
|
|||||||
set => SetValue(AllowDuplicatesProperty, value);
|
set => SetValue(AllowDuplicatesProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static TagInput()
|
||||||
|
{
|
||||||
|
InputThemeProperty.Changed.AddClassHandler<TagInput>((o, e) => o.OnInputThemePropertyChanged(e));
|
||||||
|
TagsProperty.Changed.AddClassHandler<TagInput>((o, e) => o.OnTagsPropertyChanged(e));
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
_itemsControl = e.NameScope.Find<ItemsControl>(PART_ItemsControl);
|
_itemsControl = e.NameScope.Find<ItemsControl>(PART_ItemsControl);
|
||||||
if (IsSet(InputThemeProperty) && InputTheme.TargetType == typeof(TextBox))
|
|
||||||
{
|
|
||||||
_textBox.Theme = InputTheme;
|
|
||||||
}
|
|
||||||
Items.Add(_textBox);
|
Items.Add(_textBox);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnInputThemePropertyChanged(AvaloniaPropertyChangedEventArgs args)
|
||||||
|
{
|
||||||
|
var newTheme = args.GetNewValue<ControlTheme>();
|
||||||
|
if (newTheme.TargetType == typeof(TextBox))
|
||||||
|
{
|
||||||
|
_textBox.Theme = newTheme;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnTagsPropertyChanged(AvaloniaPropertyChangedEventArgs args)
|
||||||
|
{
|
||||||
|
var newTags = args.GetNewValue<IList<string>>();
|
||||||
|
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)
|
private void OnTextBoxKeyDown(object? sender, KeyEventArgs args)
|
||||||
{
|
{
|
||||||
if (args.Key == Key.Enter)
|
if (args.Key == Key.Enter)
|
||||||
@@ -141,8 +166,7 @@ public class TagInput: TemplatedControl
|
|||||||
{
|
{
|
||||||
if (o is Control t)
|
if (o is Control t)
|
||||||
{
|
{
|
||||||
var presenter = t.Parent as ContentPresenter;
|
if (t.Parent is ContentPresenter presenter)
|
||||||
if (presenter != null)
|
|
||||||
{
|
{
|
||||||
int? index = _itemsControl?.IndexFromContainer(presenter);
|
int? index = _itemsControl?.IndexFromContainer(presenter);
|
||||||
if (index is >= 0 && index < Items.Count - 1)
|
if (index is >= 0 && index < Items.Count - 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user