Merge pull request #393 from irihitech/selectable

Introduce IsSelectable for TreeComboBoxItem.
This commit is contained in:
Dong Bin
2024-09-12 16:46:28 +08:00
committed by GitHub
5 changed files with 21 additions and 3 deletions

View File

@@ -52,6 +52,11 @@
PopupInnerTopContent="Top" PopupInnerTopContent="Top"
PopupInnerBottomContent="Bottom" PopupInnerBottomContent="Bottom"
ItemsSource="{Binding Items}"> ItemsSource="{Binding Items}">
<u:TreeComboBox.Styles>
<Style Selector="u|TreeComboBoxItem" x:DataType="vm:TreeComboBoxItemViewModel">
<Setter Property="IsSelectable" Value="{Binding IsSelectable}"/>
</Style>
</u:TreeComboBox.Styles>
<u:TreeComboBox.ItemTemplate> <u:TreeComboBox.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding Children}"> <TreeDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding ItemName}" /> <TextBlock Text="{Binding ItemName}" />

View File

@@ -19,7 +19,8 @@ public partial class TreeComboBoxDemoViewModel: ObservableObject
{ {
new TreeComboBoxItemViewModel() new TreeComboBoxItemViewModel()
{ {
ItemName = "Item 1-1", ItemName = "Item 1-1 (Not selectable)",
IsSelectable = false,
Children = new List<TreeComboBoxItemViewModel>() Children = new List<TreeComboBoxItemViewModel>()
{ {
new TreeComboBoxItemViewModel() new TreeComboBoxItemViewModel()
@@ -45,7 +46,8 @@ public partial class TreeComboBoxDemoViewModel: ObservableObject
{ {
new TreeComboBoxItemViewModel() new TreeComboBoxItemViewModel()
{ {
ItemName = "Item 2-1" ItemName = "Item 2-1 (Not selectable)",
IsSelectable = false,
}, },
new TreeComboBoxItemViewModel() new TreeComboBoxItemViewModel()
{ {
@@ -64,5 +66,6 @@ public partial class TreeComboBoxDemoViewModel: ObservableObject
public partial class TreeComboBoxItemViewModel : ObservableObject public partial class TreeComboBoxItemViewModel : ObservableObject
{ {
[ObservableProperty] private string? _itemName; [ObservableProperty] private string? _itemName;
[ObservableProperty] private bool _isSelectable = true;
public List<TreeComboBoxItemViewModel> Children { get; set; } = new (); public List<TreeComboBoxItemViewModel> Children { get; set; } = new ();
} }

View File

@@ -217,6 +217,7 @@
<Setter Property="Background" Value="{DynamicResource TreeViewItemDefaultBackground}" /> <Setter Property="Background" Value="{DynamicResource TreeViewItemDefaultBackground}" />
<Setter Property="Foreground" Value="{DynamicResource TreeViewItemDefaultForeground}" /> <Setter Property="Foreground" Value="{DynamicResource TreeViewItemDefaultForeground}" />
<Setter Property="CornerRadius" Value="3" /> <Setter Property="CornerRadius" Value="3" />
<Setter Property="MinHeight" Value="32"></Setter>
<Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate TargetType="u:TreeComboBoxItem"> <ControlTemplate TargetType="u:TreeComboBoxItem">

View File

@@ -198,7 +198,7 @@ public class TreeComboBox: ItemsControl, IClearControl, IInnerContentControl, IP
if (_popup is not null && _popup.IsOpen && _popup.IsInsidePopup(source)) if (_popup is not null && _popup.IsOpen && _popup.IsInsidePopup(source))
{ {
var container = GetContainerFromEventSource(source); var container = GetContainerFromEventSource(source);
if (container is null) return; if (container is null || !container.IsSelectable) return;
var item = TreeItemFromContainer(container); var item = TreeItemFromContainer(container);
if (item is null) return; if (item is null) return;
if (SelectedItem is not null) if (SelectedItem is not null)

View File

@@ -33,6 +33,15 @@ public class TreeComboBoxItem: HeaderedItemsControl, ISelectable
set => SetValue(IsExpandedProperty, value); set => SetValue(IsExpandedProperty, value);
} }
public static readonly StyledProperty<bool> IsSelectableProperty = AvaloniaProperty.Register<TreeComboBoxItem, bool>(
nameof(IsSelectable), true);
public bool IsSelectable
{
get => GetValue(IsSelectableProperty);
set => SetValue(IsSelectableProperty, value);
}
public static readonly DirectProperty<TreeComboBoxItem, int> LevelProperty = AvaloniaProperty.RegisterDirect<TreeComboBoxItem, int>( public static readonly DirectProperty<TreeComboBoxItem, int> LevelProperty = AvaloniaProperty.RegisterDirect<TreeComboBoxItem, int>(