Add IsItemSelectableProperty for List

This commit is contained in:
chinboy
2025-10-30 11:38:47 +08:00
parent d3cc46269c
commit fd65e37aab
4 changed files with 75 additions and 36 deletions
+9
View File
@@ -70,6 +70,9 @@ public class List : TemplatedControl,
public static readonly StyledProperty<SelectionMode> SelectionModeProperty =
AvaloniaProperty.Register<List, SelectionMode>(nameof(SelectionMode));
public static readonly StyledProperty<bool> IsItemSelectableProperty =
AvaloniaProperty.Register<List, bool>(nameof(IsItemSelectable), true);
public static readonly StyledProperty<SizeType> SizeTypeProperty =
SizeTypeAwareControlProperty.SizeTypeProperty.AddOwner<List>();
@@ -158,6 +161,12 @@ public class List : TemplatedControl,
set => SetValue(SelectionModeProperty, value);
}
public bool IsItemSelectable
{
get => GetValue(IsItemSelectableProperty);
set => SetValue(IsItemSelectableProperty, value);
}
public SizeType SizeType
{
get => GetValue(SizeTypeProperty);
+62 -33
View File
@@ -41,6 +41,9 @@ internal class ListDefaultView : SelectingItemsControl
public new static readonly StyledProperty<SelectionMode> SelectionModeProperty =
SelectingItemsControl.SelectionModeProperty;
public static readonly StyledProperty<bool> IsItemSelectableProperty =
List.IsItemSelectableProperty.AddOwner<ListDefaultView>();
public static readonly StyledProperty<SizeType> SizeTypeProperty =
SizeTypeAwareControlProperty.SizeTypeProperty.AddOwner<ListDefaultView>();
@@ -86,6 +89,12 @@ internal class ListDefaultView : SelectingItemsControl
set => base.SelectionMode = value;
}
public bool IsItemSelectable
{
get => GetValue(IsItemSelectableProperty);
set => SetValue(IsItemSelectableProperty, value);
}
public SizeType SizeType
{
get => GetValue(SizeTypeProperty);
@@ -235,33 +244,37 @@ internal class ListDefaultView : SelectingItemsControl
protected override void OnKeyDown(KeyEventArgs e)
{
var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration;
var ctrl = hotkeys is not null && e.KeyModifiers.HasAllFlags(hotkeys.CommandModifiers);
if (IsItemSelectable)
{
var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration;
var ctrl = hotkeys is not null && e.KeyModifiers.HasAllFlags(hotkeys.CommandModifiers);
if (!ctrl &&
e.Key.ToNavigationDirection() is { } direction &&
direction.IsDirectional())
{
e.Handled |= MoveSelection(
direction,
WrapSelection,
e.KeyModifiers.HasAllFlags(KeyModifiers.Shift));
}
else if (SelectionMode.HasAllFlags(SelectionMode.Multiple) &&
hotkeys is not null && hotkeys.SelectAll.Any(x => x.Matches(e)))
{
Selection.SelectAll();
e.Handled = true;
}
else if (e.Key == Key.Space || e.Key == Key.Enter)
{
UpdateSelectionFromEventSource(
e.Source,
true,
e.KeyModifiers.HasFlag(KeyModifiers.Shift),
ctrl);
}
if (!ctrl &&
e.Key.ToNavigationDirection() is { } direction &&
direction.IsDirectional())
{
e.Handled |= MoveSelection(
direction,
WrapSelection,
e.KeyModifiers.HasAllFlags(KeyModifiers.Shift));
}
else if (SelectionMode.HasAllFlags(SelectionMode.Multiple) &&
hotkeys is not null && hotkeys.SelectAll.Any(x => x.Matches(e)))
{
Selection.SelectAll();
e.Handled = true;
}
else if (e.Key == Key.Space || e.Key == Key.Enter)
{
UpdateSelectionFromEventSource(
e.Source,
true,
e.KeyModifiers.HasFlag(KeyModifiers.Shift),
ctrl);
}
}
base.OnKeyDown(e);
}
@@ -273,13 +286,29 @@ internal class ListDefaultView : SelectingItemsControl
protected internal virtual bool UpdateSelectionFromPointerEvent(Control source, PointerEventArgs e)
{
var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration;
var toggle = hotkeys is not null && e.KeyModifiers.HasAllFlags(hotkeys.CommandModifiers);
return UpdateSelectionFromEventSource(
source,
true,
e.KeyModifiers.HasAllFlags(KeyModifiers.Shift),
toggle,
e.GetCurrentPoint(source).Properties.IsRightButtonPressed);
if (IsItemSelectable)
{
var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration;
var toggle = hotkeys is not null && e.KeyModifiers.HasAllFlags(hotkeys.CommandModifiers);
return UpdateSelectionFromEventSource(
source,
true,
e.KeyModifiers.HasAllFlags(KeyModifiers.Shift),
toggle,
e.GetCurrentPoint(source).Properties.IsRightButtonPressed);
}
return false;
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == IsItemSelectableProperty)
{
if (!IsItemSelectable)
{
Selection.Clear();
}
}
}
}
@@ -52,19 +52,19 @@
<Style Selector="^[SizeType=Large]">
<Setter Property="CornerRadius" Value="{DynamicResource {x:Static atom:SharedTokenKey.BorderRadius}}" />
<Setter Property="Height" Value="{DynamicResource {x:Static atom:SharedTokenKey.ControlHeightLG}}" />
<Setter Property="MinHeight" Value="{DynamicResource {x:Static atom:SharedTokenKey.ControlHeightLG}}" />
<Setter Property="Padding" Value="{DynamicResource {x:Static atom:ListTokenKey.ItemPaddingLG}}" />
</Style>
<Style Selector="^[SizeType=Middle]">
<Setter Property="CornerRadius" Value="{DynamicResource {x:Static atom:SharedTokenKey.BorderRadiusSM}}" />
<Setter Property="Height" Value="{DynamicResource {x:Static atom:SharedTokenKey.ControlHeight}}" />
<Setter Property="MinHeight" Value="{DynamicResource {x:Static atom:SharedTokenKey.ControlHeight}}" />
<Setter Property="Padding" Value="{DynamicResource {x:Static atom:ListTokenKey.ItemPadding}}" />
</Style>
<Style Selector="^[SizeType=Small]">
<Setter Property="CornerRadius" Value="{DynamicResource {x:Static atom:SharedTokenKey.BorderRadiusXS}}" />
<Setter Property="Height" Value="{DynamicResource {x:Static atom:SharedTokenKey.ControlHeightSM}}" />
<Setter Property="MinHeight" Value="{DynamicResource {x:Static atom:SharedTokenKey.ControlHeightSM}}" />
<Setter Property="Padding" Value="{DynamicResource {x:Static atom:ListTokenKey.ItemPaddingSM}}" />
</Style>
@@ -46,6 +46,7 @@
ItemTemplate="{TemplateBinding ItemTemplate}"
SelectedItems="{TemplateBinding SelectedItems, Mode=TwoWay}"
IsShowSelectedIndicator="{TemplateBinding IsShowSelectedIndicator}"
IsItemSelectable="{TemplateBinding IsItemSelectable}"
ItemsSource="{TemplateBinding CollectionView}"/>
<ContentPresenter
Name="EmptyIndicator"