feat: add remove command support.
This commit is contained in:
@@ -5,27 +5,59 @@
|
|||||||
<!-- Add Resources Here -->
|
<!-- Add Resources Here -->
|
||||||
<ControlTheme x:Key="{x:Type u:MultiComboBox}" TargetType="u:MultiComboBox">
|
<ControlTheme x:Key="{x:Type u:MultiComboBox}" TargetType="u:MultiComboBox">
|
||||||
<Setter Property="Focusable" Value="True" />
|
<Setter Property="Focusable" Value="True" />
|
||||||
|
<Setter Property="HorizontalAlignment" Value="Left" />
|
||||||
|
<Setter Property="Width" Value="300" />
|
||||||
<Setter Property="MaxDropdownHeight" Value="300" />
|
<Setter Property="MaxDropdownHeight" Value="300" />
|
||||||
|
<Setter Property="MinHeight" Value="32" />
|
||||||
|
<Setter Property="Padding" Value="12 4" />
|
||||||
<Setter Property="Template">
|
<Setter Property="Template">
|
||||||
<ControlTemplate TargetType="u:MultiComboBox">
|
<ControlTemplate TargetType="u:MultiComboBox">
|
||||||
<Panel>
|
<Panel>
|
||||||
<ToggleButton Name="button" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}">
|
<Border
|
||||||
<ItemsControl ItemsSource="{TemplateBinding SelectedItems}">
|
HorizontalAlignment="Stretch"
|
||||||
<ItemsControl.ItemsPanel>
|
VerticalAlignment="Stretch"
|
||||||
<ItemsPanelTemplate>
|
Background="Transparent"
|
||||||
<WrapPanel Orientation="Horizontal" />
|
BorderBrush="{DynamicResource SemiGrey2}"
|
||||||
</ItemsPanelTemplate>
|
BorderThickness="1">
|
||||||
</ItemsControl.ItemsPanel>
|
<Grid Name="PART_RootGrid" ColumnDefinitions="*, Auto, 32">
|
||||||
</ItemsControl>
|
<Border
|
||||||
</ToggleButton>
|
Name="{x:Static u:MultiComboBox.PART_BackgroundBorder}"
|
||||||
|
Grid.Column="0"
|
||||||
|
Grid.ColumnSpan="3"
|
||||||
|
Background="Transparent" />
|
||||||
|
<u:MultiComboBoxSelectedItemList
|
||||||
|
Grid.Column="0"
|
||||||
|
ItemsSource="{TemplateBinding SelectedItems}"
|
||||||
|
RemoveCommand="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Remove}">
|
||||||
|
<ItemsControl.ItemsPanel>
|
||||||
|
<ItemsPanelTemplate>
|
||||||
|
<WrapPanel Orientation="Horizontal" />
|
||||||
|
</ItemsPanelTemplate>
|
||||||
|
</ItemsControl.ItemsPanel>
|
||||||
|
</u:MultiComboBoxSelectedItemList>
|
||||||
|
<PathIcon
|
||||||
|
x:Name="DropDownGlyph"
|
||||||
|
Grid.Column="2"
|
||||||
|
Width="12"
|
||||||
|
Height="12"
|
||||||
|
Margin="0,0,10,0"
|
||||||
|
HorizontalAlignment="Right"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
Data="{DynamicResource ComboBoxIcon}"
|
||||||
|
Foreground="{DynamicResource ComboBoxIconDefaultForeground}"
|
||||||
|
IsHitTestVisible="False"
|
||||||
|
UseLayoutRounding="False" />
|
||||||
|
</Grid>
|
||||||
|
</Border>
|
||||||
<Popup
|
<Popup
|
||||||
|
Width="{Binding #PART_RootGrid.Bounds.Width}"
|
||||||
MaxHeight="{TemplateBinding MaxDropdownHeight}"
|
MaxHeight="{TemplateBinding MaxDropdownHeight}"
|
||||||
IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
|
|
||||||
IsLightDismissEnabled="True"
|
IsLightDismissEnabled="True"
|
||||||
PlacementTarget="button">
|
IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
|
||||||
|
PlacementTarget="PART_RootGrid">
|
||||||
<Border Theme="{DynamicResource CardBorder}">
|
<Border Theme="{DynamicResource CardBorder}">
|
||||||
<ScrollViewer>
|
<ScrollViewer>
|
||||||
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
|
<ItemsPresenter HorizontalAlignment="Stretch" ItemsPanel="{TemplateBinding ItemsPanel}" />
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
</Border>
|
</Border>
|
||||||
</Popup>
|
</Popup>
|
||||||
@@ -36,7 +68,7 @@
|
|||||||
|
|
||||||
<ControlTheme x:Key="{x:Type u:MultiComboBoxItem}" TargetType="u:MultiComboBoxItem">
|
<ControlTheme x:Key="{x:Type u:MultiComboBoxItem}" TargetType="u:MultiComboBoxItem">
|
||||||
<Setter Property="Padding" Value="8,0,0,0" />
|
<Setter Property="Padding" Value="8,0,0,0" />
|
||||||
<Setter Property="HorizontalAlignment" Value="Left" />
|
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||||
<Setter Property="VerticalAlignment" Value="Top" />
|
<Setter Property="VerticalAlignment" Value="Top" />
|
||||||
<Setter Property="Cursor" Value="Hand" />
|
<Setter Property="Cursor" Value="Hand" />
|
||||||
<Setter Property="HorizontalContentAlignment" Value="Left" />
|
<Setter Property="HorizontalContentAlignment" Value="Left" />
|
||||||
@@ -45,7 +77,7 @@
|
|||||||
<Setter Property="CornerRadius" Value="{DynamicResource ListBoxItemCheckBoxCornerRadius}" />
|
<Setter Property="CornerRadius" Value="{DynamicResource ListBoxItemCheckBoxCornerRadius}" />
|
||||||
<Setter Property="MinHeight" Value="32" />
|
<Setter Property="MinHeight" Value="32" />
|
||||||
<Setter Property="Foreground" Value="{DynamicResource ListBoxItemCheckForeground}" />
|
<Setter Property="Foreground" Value="{DynamicResource ListBoxItemCheckForeground}" />
|
||||||
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckDefaultBackground}" />
|
<Setter Property="Background" Value="Transparent" />
|
||||||
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckDefaultBorderBrush}" />
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckDefaultBorderBrush}" />
|
||||||
<Setter Property="Template">
|
<Setter Property="Template">
|
||||||
<ControlTemplate TargetType="u:MultiComboBoxItem">
|
<ControlTemplate TargetType="u:MultiComboBoxItem">
|
||||||
@@ -83,7 +115,8 @@
|
|||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
Content="{TemplateBinding Content}"
|
Content="{TemplateBinding Content}"
|
||||||
ContentTemplate="{TemplateBinding ContentTemplate}"
|
ContentTemplate="{TemplateBinding ContentTemplate}"
|
||||||
IsVisible="{TemplateBinding Content,Converter={x:Static ObjectConverters.IsNotNull}}"
|
IsVisible="{TemplateBinding Content,
|
||||||
|
Converter={x:Static ObjectConverters.IsNotNull}}"
|
||||||
RecognizesAccessKey="True"
|
RecognizesAccessKey="True"
|
||||||
TextWrapping="Wrap" />
|
TextWrapping="Wrap" />
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -132,10 +165,8 @@
|
|||||||
<!-- Checked Pointerover State -->
|
<!-- Checked Pointerover State -->
|
||||||
<Style Selector="^:pointerover">
|
<Style Selector="^:pointerover">
|
||||||
<Style Selector="^ /template/ Border#NormalRectangle">
|
<Style Selector="^ /template/ Border#NormalRectangle">
|
||||||
<Setter Property="BorderBrush"
|
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckCheckedPointeroverBorderBrush}" />
|
||||||
Value="{DynamicResource ListBoxItemCheckCheckedPointeroverBorderBrush}" />
|
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckCheckedPointeroverBackground}" />
|
||||||
<Setter Property="Background"
|
|
||||||
Value="{DynamicResource ListBoxItemCheckCheckedPointeroverBackground}" />
|
|
||||||
</Style>
|
</Style>
|
||||||
</Style>
|
</Style>
|
||||||
|
|
||||||
@@ -163,4 +194,12 @@
|
|||||||
</Style>
|
</Style>
|
||||||
</Style>
|
</Style>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|
||||||
|
<ControlTheme x:Key="{x:Type u:MultiComboBoxSelectedItemList}" TargetType="u:MultiComboBoxSelectedItemList">
|
||||||
|
<Setter Property="Template">
|
||||||
|
<ControlTemplate TargetType="u:MultiComboBoxSelectedItemList">
|
||||||
|
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter>
|
||||||
|
</ControlTheme>
|
||||||
</ResourceDictionary>
|
</ResourceDictionary>
|
||||||
|
|||||||
@@ -42,10 +42,10 @@
|
|||||||
</Border>
|
</Border>
|
||||||
</ControlTemplate>
|
</ControlTemplate>
|
||||||
</Setter>
|
</Setter>
|
||||||
<Style Selector="^:pointerover /template/ Border#PART_RootBorder">
|
<Style Selector="^:pointerover /template/ Border#PART_BackgroundBorder">
|
||||||
<Setter Property="Border.Background" Value="{DynamicResource TextBoxPointeroverBackground}" />
|
<Setter Property="Border.Background" Value="{DynamicResource TextBoxPointeroverBackground}" />
|
||||||
</Style>
|
</Style>
|
||||||
<Style Selector="^:focus-within /template/ Border#PART_RootBorder">
|
<Style Selector="^:focus-within /template/ Border#PART_BackgroundBorder">
|
||||||
<Setter Property="Border.BorderBrush" Value="{DynamicResource TextBoxFocusBorderBrush}" />
|
<Setter Property="Border.BorderBrush" Value="{DynamicResource TextBoxFocusBorderBrush}" />
|
||||||
</Style>
|
</Style>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|||||||
@@ -7,13 +7,18 @@ 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 Avalonia.Input;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
using Irihi.Avalonia.Shared.Helpers;
|
using Irihi.Avalonia.Shared.Helpers;
|
||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
|
[TemplatePart(PART_BackgroundBorder, typeof(Border))]
|
||||||
public class MultiComboBox: SelectingItemsControl
|
public class MultiComboBox: SelectingItemsControl
|
||||||
{
|
{
|
||||||
|
public const string PART_BackgroundBorder = "PART_BackgroundBorder";
|
||||||
|
private Border? _rootBorder;
|
||||||
|
|
||||||
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 =
|
||||||
@@ -60,9 +65,18 @@ public class MultiComboBox: SelectingItemsControl
|
|||||||
return new MultiComboBoxItem();
|
return new MultiComboBoxItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
base.PrepareContainerForItemOverride(container, item, index);
|
base.OnApplyTemplate(e);
|
||||||
|
PointerPressedEvent.RemoveHandler(OnBackgroundPointerPressed, _rootBorder);
|
||||||
|
_rootBorder = e.NameScope.Find<Border>(PART_BackgroundBorder);
|
||||||
|
PointerPressedEvent.AddHandler(OnBackgroundPointerPressed, _rootBorder);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnBackgroundPointerPressed(object sender, PointerPressedEventArgs e)
|
||||||
|
{
|
||||||
|
SetCurrentValue(IsDropDownOpenProperty, !IsDropDownOpen);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ItemFocused(MultiComboBoxItem dropDownItem)
|
internal void ItemFocused(MultiComboBoxItem dropDownItem)
|
||||||
@@ -72,4 +86,23 @@ public class MultiComboBox: SelectingItemsControl
|
|||||||
dropDownItem.BringIntoView();
|
dropDownItem.BringIntoView();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Remove(object? o)
|
||||||
|
{
|
||||||
|
if (o is StyledElement s)
|
||||||
|
{
|
||||||
|
var data = s.DataContext;
|
||||||
|
this.SelectedItems?.Remove(data);
|
||||||
|
var item = this.Items.FirstOrDefault(a => ReferenceEquals(a, data));
|
||||||
|
if (item is not null)
|
||||||
|
{
|
||||||
|
var container = ContainerFromItem(item);
|
||||||
|
if (container is MultiComboBoxItem t)
|
||||||
|
{
|
||||||
|
t.IsSelected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using Avalonia;
|
using System.Windows.Input;
|
||||||
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.Mixins;
|
using Avalonia.Controls.Mixins;
|
||||||
using Avalonia.Input;
|
using Avalonia.Input;
|
||||||
|
|||||||
36
src/Ursa/Controls/ComboBox/MultiComboBoxSelectedItemList.cs
Normal file
36
src/Ursa/Controls/ComboBox/MultiComboBoxSelectedItemList.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Windows.Input;
|
||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
|
public class MultiComboBoxSelectedItemList: ItemsControl
|
||||||
|
{
|
||||||
|
public static readonly StyledProperty<ICommand?> RemoveCommandProperty = AvaloniaProperty.Register<MultiComboBoxSelectedItemList, ICommand?>(
|
||||||
|
nameof(RemoveCommand));
|
||||||
|
|
||||||
|
public ICommand? RemoveCommand
|
||||||
|
{
|
||||||
|
get => GetValue(RemoveCommandProperty);
|
||||||
|
set => SetValue(RemoveCommandProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
||||||
|
{
|
||||||
|
return NeedsContainer<ClosableTag>(item, out recycleKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
|
||||||
|
{
|
||||||
|
return new ClosableTag();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
|
||||||
|
{
|
||||||
|
base.PrepareContainerForItemOverride(container, item, index);
|
||||||
|
if (container is ClosableTag tag)
|
||||||
|
{
|
||||||
|
tag.Command = RemoveCommand;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user