feat: support clear.

This commit is contained in:
rabbitism
2024-03-26 12:05:16 +08:00
parent bc4262efc8
commit 5d917d5905
3 changed files with 229 additions and 135 deletions

View File

@@ -1,5 +1,4 @@
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Avalonia;
using Avalonia.Collections;
@@ -9,14 +8,19 @@ using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls;
[TemplatePart(PART_BackgroundBorder, typeof(Border))]
[PseudoClasses(PC_DropDownOpen, PC_Empty)]
public class MultiComboBox: SelectingItemsControl
{
public const string PART_BackgroundBorder = "PART_BackgroundBorder";
public const string PC_DropDownOpen = ":dropdownopen";
public const string PC_Empty = ":selection-empty";
private Border? _rootBorder;
private static ITemplate<Panel?> _defaultPanel = new FuncTemplate<Panel?>(() => new VirtualizingStackPanel());
@@ -52,6 +56,33 @@ public class MultiComboBox: SelectingItemsControl
{
FocusableProperty.OverrideDefaultValue<MultiComboBox>(true);
ItemsPanelProperty.OverrideDefaultValue<MultiComboBox>(_defaultPanel);
IsDropDownOpenProperty.AffectsPseudoClass<MultiComboBox>(PC_DropDownOpen);
SelectedItemsProperty.Changed.AddClassHandler<MultiComboBox, IList?>((box, args) => box.OnSelectedItemsChanged(args));
}
public MultiComboBox()
{
if (SelectedItems is INotifyCollectionChanged c)
{
c.CollectionChanged+=OnSelectedItemsCollectionChanged;
}
}
private void OnSelectedItemsChanged(AvaloniaPropertyChangedEventArgs<IList?> args)
{
if (args.OldValue.Value is INotifyCollectionChanged old)
{
old.CollectionChanged-=OnSelectedItemsCollectionChanged;
}
if (args.NewValue.Value is INotifyCollectionChanged @new)
{
@new.CollectionChanged += OnSelectedItemsCollectionChanged;
}
}
private void OnSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
PseudoClasses.Set(PC_Empty, SelectedItems?.Count == 0);
}
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
@@ -71,7 +102,7 @@ public class MultiComboBox: SelectingItemsControl
PointerPressedEvent.RemoveHandler(OnBackgroundPointerPressed, _rootBorder);
_rootBorder = e.NameScope.Find<Border>(PART_BackgroundBorder);
PointerPressedEvent.AddHandler(OnBackgroundPointerPressed, _rootBorder);
PseudoClasses.Set(PC_Empty, SelectedItems?.Count == 0);
}
private void OnBackgroundPointerPressed(object sender, PointerPressedEventArgs e)
@@ -105,4 +136,27 @@ public class MultiComboBox: SelectingItemsControl
}
}
public void Clear()
{
this.SelectedItems?.Clear();
var containers = Presenter?.Panel?.Children;
if(containers is null) return;
foreach (var container in containers)
{
if (container is MultiComboBoxItem t)
{
t.IsSelected = false;
}
}
}
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
if (SelectedItems is INotifyCollectionChanged c)
{
c.CollectionChanged-=OnSelectedItemsCollectionChanged;
}
}
}