feat: deal with virtualization.
This commit is contained in:
@@ -1,32 +1,166 @@
|
|||||||
<ResourceDictionary xmlns="https://github.com/avaloniaui"
|
<ResourceDictionary
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns="https://github.com/avaloniaui"
|
||||||
xmlns:u="https://irihi.tech/ursa">
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
<!-- Add Resources Here -->
|
xmlns:u="https://irihi.tech/ursa">
|
||||||
<ControlTheme TargetType="u:MultiComboBox" x:Key="{x:Type u:MultiComboBox}">
|
<!-- Add Resources Here -->
|
||||||
<Setter Property="Focusable" Value="True"></Setter>
|
<ControlTheme x:Key="{x:Type u:MultiComboBox}" TargetType="u:MultiComboBox">
|
||||||
|
<Setter Property="Focusable" Value="True" />
|
||||||
|
<Setter Property="MaxDropdownHeight" Value="300" />
|
||||||
<Setter Property="Template">
|
<Setter Property="Template">
|
||||||
<ControlTemplate TargetType="u:MultiComboBox">
|
<ControlTemplate TargetType="u:MultiComboBox">
|
||||||
<Panel>
|
<Panel>
|
||||||
<ToggleButton Name="button">
|
<ToggleButton Name="button" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}">
|
||||||
<ItemsControl ItemsSource="{TemplateBinding SelectedItems}">
|
<ItemsControl ItemsSource="{TemplateBinding SelectedItems}">
|
||||||
<ItemsControl.ItemsPanel>
|
<ItemsControl.ItemsPanel>
|
||||||
<ItemsPanelTemplate>
|
<ItemsPanelTemplate>
|
||||||
<WrapPanel Orientation="Horizontal"/>
|
<WrapPanel Orientation="Horizontal" />
|
||||||
</ItemsPanelTemplate>
|
</ItemsPanelTemplate>
|
||||||
</ItemsControl.ItemsPanel>
|
</ItemsControl.ItemsPanel>
|
||||||
</ItemsControl>
|
</ItemsControl>
|
||||||
</ToggleButton>
|
</ToggleButton>
|
||||||
<Popup PlacementTarget="button" IsOpen="{Binding #button.IsChecked}">
|
<Popup
|
||||||
<ListBox Name="{x:Static u:MultiComboBox.PART_ListBox}" SelectionMode="Multiple" ItemsSource="{TemplateBinding ItemsSource}" SelectedItems="{TemplateBinding SelectedItems}">
|
MaxHeight="{TemplateBinding MaxDropdownHeight}"
|
||||||
|
IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
|
||||||
</ListBox>
|
IsLightDismissEnabled="True"
|
||||||
|
PlacementTarget="button">
|
||||||
|
<Border Theme="{DynamicResource CardBorder}">
|
||||||
|
<ScrollViewer>
|
||||||
|
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
|
||||||
|
</ScrollViewer>
|
||||||
|
</Border>
|
||||||
</Popup>
|
</Popup>
|
||||||
</Panel>
|
</Panel>
|
||||||
</ControlTemplate>
|
</ControlTemplate>
|
||||||
</Setter>
|
</Setter>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|
||||||
<ControlTheme TargetType="u:MultiComboBoxItem" x:Key="{x:Type u:MultiComboBoxItem}">
|
<ControlTheme x:Key="{x:Type u:MultiComboBoxItem}" TargetType="u:MultiComboBoxItem">
|
||||||
<Setter Property="Focusable" Value="True"></Setter>
|
<Setter Property="Padding" Value="8,0,0,0" />
|
||||||
|
<Setter Property="HorizontalAlignment" Value="Left" />
|
||||||
|
<Setter Property="VerticalAlignment" Value="Top" />
|
||||||
|
<Setter Property="Cursor" Value="Hand" />
|
||||||
|
<Setter Property="HorizontalContentAlignment" Value="Left" />
|
||||||
|
<Setter Property="VerticalContentAlignment" Value="Center" />
|
||||||
|
<Setter Property="FontSize" Value="{DynamicResource ListBoxItemCheckFontSize}" />
|
||||||
|
<Setter Property="CornerRadius" Value="{DynamicResource ListBoxItemCheckBoxCornerRadius}" />
|
||||||
|
<Setter Property="MinHeight" Value="32" />
|
||||||
|
<Setter Property="Foreground" Value="{DynamicResource ListBoxItemCheckForeground}" />
|
||||||
|
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckDefaultBackground}" />
|
||||||
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckDefaultBorderBrush}" />
|
||||||
|
<Setter Property="Template">
|
||||||
|
<ControlTemplate TargetType="u:MultiComboBoxItem">
|
||||||
|
<Border
|
||||||
|
x:Name="RootBorder"
|
||||||
|
Background="{TemplateBinding Background}"
|
||||||
|
BorderBrush="{TemplateBinding BorderBrush}"
|
||||||
|
BorderThickness="{TemplateBinding BorderThickness}"
|
||||||
|
CornerRadius="{TemplateBinding CornerRadius}">
|
||||||
|
<Grid x:Name="RootGrid" ColumnDefinitions="Auto,*">
|
||||||
|
<Grid
|
||||||
|
Grid.Column="0"
|
||||||
|
Margin="0,0,0,0"
|
||||||
|
VerticalAlignment="Center">
|
||||||
|
<Border
|
||||||
|
x:Name="NormalRectangle"
|
||||||
|
Width="{DynamicResource ListBoxItemCheckBoxWidth}"
|
||||||
|
Height="{DynamicResource ListBoxItemCheckBoxHeight}"
|
||||||
|
Background="{DynamicResource ListBoxItemCheckDefaultBackground}"
|
||||||
|
BorderBrush="{DynamicResource ListBoxItemCheckDefaultBorderBrush}"
|
||||||
|
BorderThickness="{DynamicResource ListBoxItemCheckBoxBorderThickness}"
|
||||||
|
CornerRadius="{TemplateBinding CornerRadius}"
|
||||||
|
UseLayoutRounding="False" />
|
||||||
|
<PathIcon
|
||||||
|
Name="CheckGlyph"
|
||||||
|
Width="{DynamicResource ListBoxItemCheckBoxGlyphWidth}"
|
||||||
|
Height="{DynamicResource ListBoxItemCheckBoxGlyphHeight}"
|
||||||
|
Foreground="{DynamicResource ListBoxItemCheckGlyphFill}" />
|
||||||
|
</Grid>
|
||||||
|
<ContentPresenter
|
||||||
|
x:Name="ContentPresenter"
|
||||||
|
Grid.Column="1"
|
||||||
|
Margin="{TemplateBinding Padding}"
|
||||||
|
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
Content="{TemplateBinding Content}"
|
||||||
|
ContentTemplate="{TemplateBinding ContentTemplate}"
|
||||||
|
IsVisible="{TemplateBinding Content,Converter={x:Static ObjectConverters.IsNotNull}}"
|
||||||
|
RecognizesAccessKey="True"
|
||||||
|
TextWrapping="Wrap" />
|
||||||
|
</Grid>
|
||||||
|
</Border>
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter>
|
||||||
|
|
||||||
|
<!-- Unchecked Pointerover State -->
|
||||||
|
<Style Selector="^:pointerover">
|
||||||
|
<Style Selector="^ /template/ Border#NormalRectangle">
|
||||||
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckPointeroverBorderBrush}" />
|
||||||
|
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckPointeroverBackground}" />
|
||||||
|
</Style>
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<!-- Unchecked Pressed State -->
|
||||||
|
<Style Selector="^:pressed">
|
||||||
|
<Style Selector="^ /template/ Border#NormalRectangle">
|
||||||
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckPressedBorderBrush}" />
|
||||||
|
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckPressedBackground}" />
|
||||||
|
</Style>
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<!-- Unchecked Disabled state -->
|
||||||
|
<Style Selector="^:disabled">
|
||||||
|
<Style Selector="^ /template/ ContentPresenter#ContentPresenter">
|
||||||
|
<Setter Property="Foreground" Value="{DynamicResource ListBoxItemCheckDisabledForeground}" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="^ /template/ Border#NormalRectangle">
|
||||||
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckDefaultDisabledBorderBrush}" />
|
||||||
|
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckDefaultDisabledBackground}" />
|
||||||
|
</Style>
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<Style Selector="^:selected">
|
||||||
|
<Style Selector="^ /template/ Border#NormalRectangle">
|
||||||
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckCheckedDefaultBorderBrush}" />
|
||||||
|
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckCheckedDefaultBackground}" />
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<Style Selector="^ /template/ PathIcon#CheckGlyph">
|
||||||
|
<Setter Property="Data" Value="{DynamicResource ListBoxItemCheckCheckGlyph}" />
|
||||||
|
<Setter Property="Opacity" Value="1" />
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<!-- Checked Pointerover State -->
|
||||||
|
<Style Selector="^:pointerover">
|
||||||
|
<Style Selector="^ /template/ Border#NormalRectangle">
|
||||||
|
<Setter Property="BorderBrush"
|
||||||
|
Value="{DynamicResource ListBoxItemCheckCheckedPointeroverBorderBrush}" />
|
||||||
|
<Setter Property="Background"
|
||||||
|
Value="{DynamicResource ListBoxItemCheckCheckedPointeroverBackground}" />
|
||||||
|
</Style>
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<!-- Checked Pressed State -->
|
||||||
|
<Style Selector="^:pressed">
|
||||||
|
<Style Selector="^ /template/ Border#NormalRectangle">
|
||||||
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckCheckedPressedBorderBrush}" />
|
||||||
|
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckCheckedPressedBackground}" />
|
||||||
|
</Style>
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<!-- Checked Disabled State -->
|
||||||
|
<Style Selector="^:disabled">
|
||||||
|
<Style Selector="^ /template/ ContentPresenter#ContentPresenter">
|
||||||
|
<Setter Property="Foreground" Value="{DynamicResource ListBoxItemCheckDisabledForeground}" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="^ /template/ Border#NormalRectangle">
|
||||||
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckCheckedDisabledBorderBrush}" />
|
||||||
|
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckCheckedDisabledBackground}" />
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
<Style Selector="^ /template/ Path#CheckGlyph">
|
||||||
|
<Setter Property="Fill" Value="{DynamicResource ListBoxItemCheckGlyphDisabledFill}" />
|
||||||
|
</Style>
|
||||||
|
</Style>
|
||||||
|
</Style>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
</ResourceDictionary>
|
</ResourceDictionary>
|
||||||
|
|||||||
@@ -2,19 +2,17 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
|
using Avalonia.Collections;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.Metadata;
|
using Avalonia.Controls.Metadata;
|
||||||
using Avalonia.Controls.Primitives;
|
using Avalonia.Controls.Primitives;
|
||||||
using Avalonia.Controls.Templates;
|
using Avalonia.Controls.Templates;
|
||||||
|
using Irihi.Avalonia.Shared.Helpers;
|
||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
[TemplatePart( PART_ListBox, typeof(ListBox))]
|
|
||||||
public class MultiComboBox: SelectingItemsControl
|
public class MultiComboBox: SelectingItemsControl
|
||||||
{
|
{
|
||||||
public const string PART_ListBox = "PART_ListBox";
|
|
||||||
private ListBox? _listBox;
|
|
||||||
|
|
||||||
private static 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 =
|
||||||
@@ -26,8 +24,17 @@ public class MultiComboBox: SelectingItemsControl
|
|||||||
set => SetValue(IsDropDownOpenProperty, value);
|
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 new static readonly StyledProperty<IList?> SelectedItemsProperty = AvaloniaProperty.Register<MultiComboBox, IList?>(
|
public new static readonly StyledProperty<IList?> SelectedItemsProperty = AvaloniaProperty.Register<MultiComboBox, IList?>(
|
||||||
nameof(SelectedItems));
|
nameof(SelectedItems), new AvaloniaList<object>());
|
||||||
|
|
||||||
public new IList? SelectedItems
|
public new IList? SelectedItems
|
||||||
{
|
{
|
||||||
@@ -43,7 +50,8 @@ public class MultiComboBox: SelectingItemsControl
|
|||||||
|
|
||||||
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
||||||
{
|
{
|
||||||
return NeedsContainer<MultiComboBoxItem>(item, out recycleKey);
|
recycleKey = item;
|
||||||
|
return item is not MultiComboBoxItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
|
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
|
||||||
@@ -51,6 +59,37 @@ public class MultiComboBox: SelectingItemsControl
|
|||||||
return new MultiComboBoxItem();
|
return new MultiComboBoxItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Dictionary<int, IDisposable?> _disposables = new Dictionary<int, IDisposable?>();
|
||||||
|
|
||||||
|
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
|
||||||
|
{
|
||||||
|
base.PrepareContainerForItemOverride(container, item, index);
|
||||||
|
if(_disposables.TryGetValue(index, out var d))
|
||||||
|
{
|
||||||
|
d?.Dispose();
|
||||||
|
_disposables.Remove(index);
|
||||||
|
}
|
||||||
|
if (container is MultiComboBoxItem comboBoxItem)
|
||||||
|
{
|
||||||
|
comboBoxItem.IsSelected = SelectedItems?.Contains(item) ?? false;
|
||||||
|
var disposable = MultiComboBoxItem.IsSelectedProperty.Changed.Subscribe(a =>
|
||||||
|
{
|
||||||
|
if (a.Sender == comboBoxItem)
|
||||||
|
{
|
||||||
|
if (comboBoxItem.IsSelected)
|
||||||
|
{
|
||||||
|
SelectedItems?.Add(item);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SelectedItems?.Remove(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
_disposables[index] = disposable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal void ItemFocused(MultiComboBoxItem dropDownItem)
|
internal void ItemFocused(MultiComboBoxItem dropDownItem)
|
||||||
{
|
{
|
||||||
if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid)
|
if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid)
|
||||||
@@ -59,21 +98,4 @@ public class MultiComboBox: SelectingItemsControl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
|
||||||
{
|
|
||||||
base.OnApplyTemplate(e);
|
|
||||||
_listBox = e.NameScope.Find<ListBox>(PART_ListBox);
|
|
||||||
if (_listBox != null)
|
|
||||||
{
|
|
||||||
_listBox.SelectionChanged += ListBox_SelectionChanged;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
||||||
{
|
|
||||||
if (sender == _listBox)
|
|
||||||
{
|
|
||||||
this.SelectedItems = _listBox.SelectedItems;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,28 @@
|
|||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Controls.Mixins;
|
||||||
|
using Avalonia.Input;
|
||||||
using Irihi.Avalonia.Shared.Helpers;
|
using Irihi.Avalonia.Shared.Helpers;
|
||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
public class MultiComboBoxItem: ListBoxItem
|
public class MultiComboBoxItem: ContentControl
|
||||||
{
|
{
|
||||||
|
public static readonly StyledProperty<bool> IsSelectedProperty = AvaloniaProperty.Register<MultiComboBoxItem, bool>(
|
||||||
|
nameof(IsSelected));
|
||||||
|
|
||||||
|
public bool IsSelected
|
||||||
|
{
|
||||||
|
get => GetValue(IsSelectedProperty);
|
||||||
|
set => SetValue(IsSelectedProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static MultiComboBoxItem()
|
||||||
|
{
|
||||||
|
IsSelectedProperty.AffectsPseudoClass<MultiComboBoxItem>(":selected");
|
||||||
|
PressedMixin.Attach<MultiComboBoxItem>();
|
||||||
|
FocusableProperty.OverrideDefaultValue<MultiComboBoxItem>(true);
|
||||||
|
}
|
||||||
public MultiComboBoxItem()
|
public MultiComboBoxItem()
|
||||||
{
|
{
|
||||||
this.GetObservable(IsFocusedProperty).Subscribe(a=> {
|
this.GetObservable(IsFocusedProperty).Subscribe(a=> {
|
||||||
@@ -15,4 +32,18 @@ public class MultiComboBoxItem: ListBoxItem
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnPointerPressed(e);
|
||||||
|
if (e.Handled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
|
||||||
|
{
|
||||||
|
this.IsSelected = !this.IsSelected;
|
||||||
|
e.Handled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user