feat: 1. update container state from selection collection change.
2. Add popup slot. 3. fix various binding relative resource issue. 4. update empty pseudo-class handing, simplify watermark visibility.
This commit is contained in:
@@ -8,128 +8,159 @@ using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
using Irihi.Avalonia.Shared.Contracts;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
[TemplatePart(PART_BackgroundBorder, typeof(Border))]
|
||||
[PseudoClasses(PC_DropDownOpen, PC_Empty)]
|
||||
public class MultiComboBox: SelectingItemsControl, IInnerContentControl
|
||||
public class MultiComboBox : SelectingItemsControl, IInnerContentControl, IPopupInnerContent
|
||||
{
|
||||
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());
|
||||
|
||||
private static readonly ITemplate<Panel?> _defaultPanel =
|
||||
new FuncTemplate<Panel?>(() => new VirtualizingStackPanel());
|
||||
|
||||
public static readonly StyledProperty<bool> IsDropDownOpenProperty =
|
||||
ComboBox.IsDropDownOpenProperty.AddOwner<MultiComboBox>();
|
||||
|
||||
|
||||
public static readonly StyledProperty<double> MaxDropdownHeightProperty =
|
||||
AvaloniaProperty.Register<MultiComboBox, double>(
|
||||
nameof(MaxDropdownHeight));
|
||||
|
||||
public static readonly StyledProperty<double> MaxSelectionBoxHeightProperty =
|
||||
AvaloniaProperty.Register<MultiComboBox, double>(
|
||||
nameof(MaxSelectionBoxHeight));
|
||||
|
||||
public new static readonly StyledProperty<IList?> SelectedItemsProperty =
|
||||
AvaloniaProperty.Register<MultiComboBox, IList?>(
|
||||
nameof(SelectedItems));
|
||||
|
||||
public static readonly StyledProperty<object?> InnerLeftContentProperty =
|
||||
AvaloniaProperty.Register<MultiComboBox, object?>(
|
||||
nameof(InnerLeftContent));
|
||||
|
||||
public static readonly StyledProperty<object?> InnerRightContentProperty =
|
||||
AvaloniaProperty.Register<MultiComboBox, object?>(
|
||||
nameof(InnerRightContent));
|
||||
|
||||
|
||||
public static readonly StyledProperty<IDataTemplate?> SelectedItemTemplateProperty =
|
||||
AvaloniaProperty.Register<MultiComboBox, IDataTemplate?>(
|
||||
nameof(SelectedItemTemplate));
|
||||
|
||||
public static readonly StyledProperty<string?> WatermarkProperty =
|
||||
TextBox.WatermarkProperty.AddOwner<MultiComboBox>();
|
||||
|
||||
public static readonly StyledProperty<object?> PopupInnerTopContentProperty =
|
||||
AvaloniaProperty.Register<MultiComboBox, object?>(
|
||||
nameof(PopupInnerTopContent));
|
||||
|
||||
public static readonly StyledProperty<object?> PopupInnerBottomContentProperty =
|
||||
AvaloniaProperty.Register<MultiComboBox, object?>(
|
||||
nameof(PopupInnerBottomContent));
|
||||
|
||||
private Border? _rootBorder;
|
||||
|
||||
static MultiComboBox()
|
||||
{
|
||||
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()
|
||||
{
|
||||
SelectedItems = new AvaloniaList<object>();
|
||||
if (SelectedItems is INotifyCollectionChanged c) c.CollectionChanged += OnSelectedItemsCollectionChanged;
|
||||
}
|
||||
|
||||
public bool IsDropDownOpen
|
||||
{
|
||||
get => GetValue(IsDropDownOpenProperty);
|
||||
set => SetValue(IsDropDownOpenProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<double> MaxDropdownHeightProperty = AvaloniaProperty.Register<MultiComboBox, double>(
|
||||
nameof(MaxDropdownHeight));
|
||||
|
||||
public double MaxDropdownHeight
|
||||
{
|
||||
get => GetValue(MaxDropdownHeightProperty);
|
||||
set => SetValue(MaxDropdownHeightProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<double> MaxSelectionBoxHeightProperty = AvaloniaProperty.Register<MultiComboBox, double>(
|
||||
nameof(MaxSelectionBoxHeight));
|
||||
|
||||
public double MaxSelectionBoxHeight
|
||||
{
|
||||
get => GetValue(MaxSelectionBoxHeightProperty);
|
||||
set => SetValue(MaxSelectionBoxHeightProperty, value);
|
||||
}
|
||||
|
||||
public new static readonly StyledProperty<IList?> SelectedItemsProperty = AvaloniaProperty.Register<MultiComboBox, IList?>(
|
||||
nameof(SelectedItems));
|
||||
|
||||
public new IList? SelectedItems
|
||||
{
|
||||
get => GetValue(SelectedItemsProperty);
|
||||
set => SetValue(SelectedItemsProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<object?> InnerLeftContentProperty = AvaloniaProperty.Register<MultiComboBox, object?>(
|
||||
nameof(InnerLeftContent));
|
||||
|
||||
public object? InnerLeftContent
|
||||
{
|
||||
get => GetValue(InnerLeftContentProperty);
|
||||
set => SetValue(InnerLeftContentProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<object?> InnerRightContentProperty = AvaloniaProperty.Register<MultiComboBox, object?>(
|
||||
nameof(InnerRightContent));
|
||||
|
||||
public object? InnerRightContent
|
||||
{
|
||||
get => GetValue(InnerRightContentProperty);
|
||||
set => SetValue(InnerRightContentProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IDataTemplate?> SelectedItemTemplateProperty = AvaloniaProperty.Register<MultiComboBox, IDataTemplate?>(
|
||||
nameof(SelectedItemTemplate));
|
||||
|
||||
public IDataTemplate? SelectedItemTemplate
|
||||
{
|
||||
get => GetValue(SelectedItemTemplateProperty);
|
||||
set => SetValue(SelectedItemTemplateProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<string?> WatermarkProperty =
|
||||
TextBox.WatermarkProperty.AddOwner<MultiComboBox>();
|
||||
|
||||
public string? Watermark
|
||||
{
|
||||
get => GetValue(WatermarkProperty);
|
||||
set => SetValue(WatermarkProperty, value);
|
||||
}
|
||||
|
||||
static MultiComboBox()
|
||||
public object? InnerLeftContent
|
||||
{
|
||||
FocusableProperty.OverrideDefaultValue<MultiComboBox>(true);
|
||||
ItemsPanelProperty.OverrideDefaultValue<MultiComboBox>(_defaultPanel);
|
||||
IsDropDownOpenProperty.AffectsPseudoClass<MultiComboBox>(PC_DropDownOpen);
|
||||
SelectedItemsProperty.Changed.AddClassHandler<MultiComboBox, IList?>((box, args) => box.OnSelectedItemsChanged(args));
|
||||
get => GetValue(InnerLeftContentProperty);
|
||||
set => SetValue(InnerLeftContentProperty, value);
|
||||
}
|
||||
|
||||
public MultiComboBox()
|
||||
public object? InnerRightContent
|
||||
{
|
||||
SelectedItems = new AvaloniaList<object>();
|
||||
if (SelectedItems is INotifyCollectionChanged c)
|
||||
{
|
||||
c.CollectionChanged+= OnSelectedItemsCollectionChanged;
|
||||
}
|
||||
|
||||
get => GetValue(InnerRightContentProperty);
|
||||
set => SetValue(InnerRightContentProperty, value);
|
||||
}
|
||||
|
||||
public object? PopupInnerTopContent
|
||||
{
|
||||
get => GetValue(PopupInnerTopContentProperty);
|
||||
set => SetValue(PopupInnerTopContentProperty, value);
|
||||
}
|
||||
|
||||
public object? PopupInnerBottomContent
|
||||
{
|
||||
get => GetValue(PopupInnerBottomContentProperty);
|
||||
set => SetValue(PopupInnerBottomContentProperty, value);
|
||||
}
|
||||
|
||||
private void OnSelectedItemsChanged(AvaloniaPropertyChangedEventArgs<IList?> args)
|
||||
{
|
||||
if (args.OldValue.Value is INotifyCollectionChanged old)
|
||||
{
|
||||
old.CollectionChanged-=OnSelectedItemsCollectionChanged;
|
||||
}
|
||||
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);
|
||||
PseudoClasses.Set(PC_Empty, SelectedItems?.Count is null or 0);
|
||||
//return;
|
||||
var containers = Presenter?.Panel?.Children;
|
||||
if (containers is null) return;
|
||||
foreach (var container in containers)
|
||||
{
|
||||
if (container is MultiComboBoxItem i)
|
||||
{
|
||||
i.UpdateSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
||||
@@ -159,10 +190,7 @@ public class MultiComboBox: SelectingItemsControl, IInnerContentControl
|
||||
|
||||
internal void ItemFocused(MultiComboBoxItem dropDownItem)
|
||||
{
|
||||
if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid)
|
||||
{
|
||||
dropDownItem.BringIntoView();
|
||||
}
|
||||
if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid) dropDownItem.BringIntoView();
|
||||
}
|
||||
|
||||
public void Remove(object? o)
|
||||
@@ -170,40 +198,29 @@ public class MultiComboBox: SelectingItemsControl, IInnerContentControl
|
||||
if (o is StyledElement s)
|
||||
{
|
||||
var data = s.DataContext;
|
||||
this.SelectedItems?.Remove(data);
|
||||
var item = this.Items.FirstOrDefault(a => ReferenceEquals(a, data));
|
||||
SelectedItems?.Remove(data);
|
||||
var item = Items.FirstOrDefault(a => ReferenceEquals(a, data));
|
||||
if (item is not null)
|
||||
{
|
||||
var container = ContainerFromItem(item);
|
||||
if (container is MultiComboBoxItem t)
|
||||
{
|
||||
t.IsSelected = false;
|
||||
}
|
||||
if (container is MultiComboBoxItem t) t.IsSelected = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
// this.SelectedItems?.Clear();
|
||||
this.SelectedItems?.Clear();
|
||||
var containers = Presenter?.Panel?.Children;
|
||||
if(containers is null) return;
|
||||
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;
|
||||
}
|
||||
if (SelectedItems is INotifyCollectionChanged c) c.CollectionChanged -= OnSelectedItemsCollectionChanged;
|
||||
}
|
||||
}
|
||||
@@ -100,6 +100,11 @@ public class MultiComboBoxItem: ContentControl
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
UpdateSelection();
|
||||
}
|
||||
|
||||
internal void UpdateSelection()
|
||||
{
|
||||
_updateInternal = true;
|
||||
if (_parent?.ItemsPanelRoot is VirtualizingPanel)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user