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:
rabbitism
2024-08-24 12:59:58 +08:00
parent fc26ec7ce5
commit dffdcf3aa3
5 changed files with 174 additions and 95 deletions

View File

@@ -12,6 +12,12 @@
x:DataType="vm:MultiComboBoxDemoViewModel" x:DataType="vm:MultiComboBoxDemoViewModel"
mc:Ignorable="d"> mc:Ignorable="d">
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<u:MultiComboBox
Watermark="Please Select"
MaxHeight="200"
SelectedItems="{Binding SelectedItems}"
ItemsSource="{Binding Items}" >
</u:MultiComboBox>
<u:MultiComboBox <u:MultiComboBox
Name="combo" Name="combo"
Watermark="Please Select" Watermark="Please Select"
@@ -19,8 +25,21 @@
InnerRightContent="Right" InnerRightContent="Right"
Classes="ClearButton" Classes="ClearButton"
MaxHeight="200" MaxHeight="200"
SelectedItems="{Binding SelectedItems}"
ItemsSource="{Binding Items}" > ItemsSource="{Binding Items}" >
<u:MultiComboBox.PopupInnerTopContent>
<StackPanel Margin="0" Orientation="Horizontal">
<Button Theme="{DynamicResource BorderlessButton}" Content="Select All" Command="{Binding SelectAllCommand}"/>
<Button Theme="{DynamicResource BorderlessButton}" Content="Unselect All" Command="{Binding ClearAllCommand}"/>
<Button Theme="{DynamicResource BorderlessButton}" Content="Inverse" Command="{Binding InvertSelectionCommand}"/>
</StackPanel>
</u:MultiComboBox.PopupInnerTopContent>
<u:MultiComboBox.ContextFlyout>
<MenuFlyout>
<MenuItem Header="Select All" Command="{Binding SelectAllCommand}"/>
</MenuFlyout>
</u:MultiComboBox.ContextFlyout>
</u:MultiComboBox> </u:MultiComboBox>
<ListBox ItemsSource="{Binding #combo.SelectedItems}" /> <ListBox ItemsSource="{Binding SelectedItems}" />
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -1,5 +1,8 @@
using System.Collections.ObjectModel; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace Ursa.Demo.ViewModels; namespace Ursa.Demo.ViewModels;
@@ -7,6 +10,35 @@ public class MultiComboBoxDemoViewModel: ObservableObject
{ {
public ObservableCollection<string> Items { get; set; } public ObservableCollection<string> Items { get; set; }
public ObservableCollection<string> SelectedItems { get; set; }
public ICommand SelectAllCommand => new RelayCommand(() =>
{
SelectedItems.Clear();
foreach (var item in Items)
{
SelectedItems.Add(item);
}
});
public ICommand ClearAllCommand => new RelayCommand(() =>
{
SelectedItems.Clear();
});
public ICommand InvertSelectionCommand => new RelayCommand(() =>
{
var selectedItems = new List<string>(SelectedItems);
SelectedItems.Clear();
foreach (var item in Items)
{
if (!selectedItems.Contains(item))
{
SelectedItems.Add(item);
}
}
});
public MultiComboBoxDemoViewModel() public MultiComboBoxDemoViewModel()
{ {
Items = new ObservableCollection<string>() Items = new ObservableCollection<string>()
@@ -47,5 +79,6 @@ public class MultiComboBoxDemoViewModel: ObservableObject
"Pennsylvania", "Pennsylvania",
"Rhode Island", "Rhode Island",
}; };
SelectedItems = new ObservableCollection<string>();
} }
} }

View File

@@ -51,6 +51,7 @@
HorizontalAlignment="Left" HorizontalAlignment="Left"
VerticalAlignment="Center" VerticalAlignment="Center"
Foreground="{TemplateBinding Foreground}" Foreground="{TemplateBinding Foreground}"
IsHitTestVisible="False"
IsVisible="False" IsVisible="False"
Opacity="0.3" Opacity="0.3"
Text="{TemplateBinding Watermark}" /> Text="{TemplateBinding Watermark}" />
@@ -104,7 +105,7 @@
</Grid> </Grid>
</Border> </Border>
<Popup <Popup
Width="{Binding #PART_RootGrid.Bounds.Width}" MinWidth="{Binding #PART_RootGrid.Bounds.Width}"
MaxHeight="{TemplateBinding MaxDropdownHeight}" MaxHeight="{TemplateBinding MaxDropdownHeight}"
IsLightDismissEnabled="True" IsLightDismissEnabled="True"
IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}" IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
@@ -118,6 +119,9 @@
BoxShadow="{DynamicResource ComboBoxPopupBoxShadow}" BoxShadow="{DynamicResource ComboBoxPopupBoxShadow}"
ClipToBounds="True" ClipToBounds="True"
CornerRadius="6"> CornerRadius="6">
<DockPanel LastChildFill="True">
<ContentPresenter Content="{TemplateBinding PopupInnerTopContent}" DockPanel.Dock="Top"/>
<ContentPresenter Content="{TemplateBinding PopupInnerBottomContent}" DockPanel.Dock="Bottom"/>
<ScrollViewer <ScrollViewer
Grid.IsSharedSizeScope="True" Grid.IsSharedSizeScope="True"
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
@@ -128,6 +132,7 @@
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
ItemsPanel="{TemplateBinding ItemsPanel}" /> ItemsPanel="{TemplateBinding ItemsPanel}" />
</ScrollViewer> </ScrollViewer>
</DockPanel>
</Border> </Border>
</Popup> </Popup>
</Panel> </Panel>
@@ -143,12 +148,12 @@
<Style Selector="^.clearButton, ^.ClearButton"> <Style Selector="^.clearButton, ^.ClearButton">
<Style Selector="^:pointerover:not(:selection-empty) /template/ Button#ClearButton"> <Style Selector="^:pointerover:not(:selection-empty) /template/ Button#ClearButton">
<Setter Property="IsVisible" Value="{Binding $parent[ComboBox].SelectionBoxItem, Converter={x:Static ObjectConverters.IsNotNull}}" /> <Setter Property="IsVisible" Value="True" />
</Style> </Style>
</Style> </Style>
<Style Selector="^:selection-empty /template/ TextBlock#PlaceholderTextBlock"> <Style Selector="^:selection-empty /template/ TextBlock#PlaceholderTextBlock">
<Setter Property="IsVisible" Value="{Binding $parent[ComboBox].SelectionBoxItem, Converter={x:Static ObjectConverters.IsNotNull}}" /> <Setter Property="IsVisible" Value="True" />
</Style> </Style>
<!-- Pointerover State --> <!-- Pointerover State -->

View File

@@ -8,128 +8,159 @@ using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Irihi.Avalonia.Shared.Helpers;
using Irihi.Avalonia.Shared.Contracts; using Irihi.Avalonia.Shared.Contracts;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls; namespace Ursa.Controls;
[TemplatePart(PART_BackgroundBorder, typeof(Border))] [TemplatePart(PART_BackgroundBorder, typeof(Border))]
[PseudoClasses(PC_DropDownOpen, PC_Empty)] [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 PART_BackgroundBorder = "PART_BackgroundBorder";
public const string PC_DropDownOpen = ":dropdownopen"; public const string PC_DropDownOpen = ":dropdownopen";
public const string PC_Empty = ":selection-empty"; public const string PC_Empty = ":selection-empty";
private Border? _rootBorder; private static readonly ITemplate<Panel?> _defaultPanel =
new FuncTemplate<Panel?>(() => new VirtualizingStackPanel());
private static ITemplate<Panel?> _defaultPanel = new FuncTemplate<Panel?>(() => new VirtualizingStackPanel());
public static readonly StyledProperty<bool> IsDropDownOpenProperty = public static readonly StyledProperty<bool> IsDropDownOpenProperty =
ComboBox.IsDropDownOpenProperty.AddOwner<MultiComboBox>(); 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 public bool IsDropDownOpen
{ {
get => GetValue(IsDropDownOpenProperty); get => GetValue(IsDropDownOpenProperty);
set => SetValue(IsDropDownOpenProperty, value); set => SetValue(IsDropDownOpenProperty, value);
} }
public static readonly StyledProperty<double> MaxDropdownHeightProperty = AvaloniaProperty.Register<MultiComboBox, double>(
nameof(MaxDropdownHeight));
public double MaxDropdownHeight public double MaxDropdownHeight
{ {
get => GetValue(MaxDropdownHeightProperty); get => GetValue(MaxDropdownHeightProperty);
set => SetValue(MaxDropdownHeightProperty, value); set => SetValue(MaxDropdownHeightProperty, value);
} }
public static readonly StyledProperty<double> MaxSelectionBoxHeightProperty = AvaloniaProperty.Register<MultiComboBox, double>(
nameof(MaxSelectionBoxHeight));
public double MaxSelectionBoxHeight public double MaxSelectionBoxHeight
{ {
get => GetValue(MaxSelectionBoxHeightProperty); get => GetValue(MaxSelectionBoxHeightProperty);
set => SetValue(MaxSelectionBoxHeightProperty, value); set => SetValue(MaxSelectionBoxHeightProperty, value);
} }
public new static readonly StyledProperty<IList?> SelectedItemsProperty = AvaloniaProperty.Register<MultiComboBox, IList?>(
nameof(SelectedItems));
public new IList? SelectedItems public new IList? SelectedItems
{ {
get => GetValue(SelectedItemsProperty); get => GetValue(SelectedItemsProperty);
set => SetValue(SelectedItemsProperty, value); 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 public IDataTemplate? SelectedItemTemplate
{ {
get => GetValue(SelectedItemTemplateProperty); get => GetValue(SelectedItemTemplateProperty);
set => SetValue(SelectedItemTemplateProperty, value); set => SetValue(SelectedItemTemplateProperty, value);
} }
public static readonly StyledProperty<string?> WatermarkProperty =
TextBox.WatermarkProperty.AddOwner<MultiComboBox>();
public string? Watermark public string? Watermark
{ {
get => GetValue(WatermarkProperty); get => GetValue(WatermarkProperty);
set => SetValue(WatermarkProperty, value); set => SetValue(WatermarkProperty, value);
} }
static MultiComboBox() public object? InnerLeftContent
{ {
FocusableProperty.OverrideDefaultValue<MultiComboBox>(true); get => GetValue(InnerLeftContentProperty);
ItemsPanelProperty.OverrideDefaultValue<MultiComboBox>(_defaultPanel); set => SetValue(InnerLeftContentProperty, value);
IsDropDownOpenProperty.AffectsPseudoClass<MultiComboBox>(PC_DropDownOpen);
SelectedItemsProperty.Changed.AddClassHandler<MultiComboBox, IList?>((box, args) => box.OnSelectedItemsChanged(args));
} }
public MultiComboBox() public object? InnerRightContent
{ {
SelectedItems = new AvaloniaList<object>(); get => GetValue(InnerRightContentProperty);
if (SelectedItems is INotifyCollectionChanged c) set => SetValue(InnerRightContentProperty, value);
{
c.CollectionChanged+= OnSelectedItemsCollectionChanged;
} }
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) private void OnSelectedItemsChanged(AvaloniaPropertyChangedEventArgs<IList?> args)
{ {
if (args.OldValue.Value is INotifyCollectionChanged old) if (args.OldValue.Value is INotifyCollectionChanged old)
{
old.CollectionChanged -= OnSelectedItemsCollectionChanged; old.CollectionChanged -= OnSelectedItemsCollectionChanged;
}
if (args.NewValue.Value is INotifyCollectionChanged @new) if (args.NewValue.Value is INotifyCollectionChanged @new)
{
@new.CollectionChanged += OnSelectedItemsCollectionChanged; @new.CollectionChanged += OnSelectedItemsCollectionChanged;
} }
}
private void OnSelectedItemsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) 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) 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) internal void ItemFocused(MultiComboBoxItem dropDownItem)
{ {
if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid) if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid) dropDownItem.BringIntoView();
{
dropDownItem.BringIntoView();
}
} }
public void Remove(object? o) public void Remove(object? o)
@@ -170,40 +198,29 @@ public class MultiComboBox: SelectingItemsControl, IInnerContentControl
if (o is StyledElement s) if (o is StyledElement s)
{ {
var data = s.DataContext; var data = s.DataContext;
this.SelectedItems?.Remove(data); SelectedItems?.Remove(data);
var item = this.Items.FirstOrDefault(a => ReferenceEquals(a, data)); var item = Items.FirstOrDefault(a => ReferenceEquals(a, data));
if (item is not null) if (item is not null)
{ {
var container = ContainerFromItem(item); var container = ContainerFromItem(item);
if (container is MultiComboBoxItem t) if (container is MultiComboBoxItem t) t.IsSelected = false;
{
t.IsSelected = false;
} }
} }
} }
}
public void Clear() public void Clear()
{ {
// this.SelectedItems?.Clear(); this.SelectedItems?.Clear();
var containers = Presenter?.Panel?.Children; var containers = Presenter?.Panel?.Children;
if (containers is null) return; if (containers is null) return;
foreach (var container in containers) foreach (var container in containers)
{
if (container is MultiComboBoxItem t) if (container is MultiComboBoxItem t)
{
t.IsSelected = false; t.IsSelected = false;
} }
}
}
protected override void OnUnloaded(RoutedEventArgs e) protected override void OnUnloaded(RoutedEventArgs e)
{ {
base.OnUnloaded(e); base.OnUnloaded(e);
if (SelectedItems is INotifyCollectionChanged c) if (SelectedItems is INotifyCollectionChanged c) c.CollectionChanged -= OnSelectedItemsCollectionChanged;
{
c.CollectionChanged-=OnSelectedItemsCollectionChanged;
}
} }
} }

View File

@@ -100,6 +100,11 @@ public class MultiComboBoxItem: ContentControl
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
UpdateSelection();
}
internal void UpdateSelection()
{
_updateInternal = true; _updateInternal = true;
if (_parent?.ItemsPanelRoot is VirtualizingPanel) if (_parent?.ItemsPanelRoot is VirtualizingPanel)
{ {