feat: Add ButtonGroup control and styles.

This commit is contained in:
NaBian committed 2026-06-13 19:21:15 +08:00
1 parent 2ac7dfaf96
commit ee5c47a7cd
9 files changed
+690 -36

No files matched your search

@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:ClassModifier="internal"
xmlns:hc="clr-namespace:HandyControl.Controls">
@@ -455,6 +455,117 @@
Value="12" />
</ControlTheme>
<ControlTheme x:Key="ButtonGroupItemBaseStyle"
TargetType="Button">
<Setter Property="Background"
Value="{DynamicResource RegionBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource BorderBrush}" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="VerticalAlignment"
Value="Center" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
<Setter Property="hc:BorderElement.CornerRadius"
Value="0" />
<Setter Property="CornerRadius"
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
<Setter Property="Height"
Value="{StaticResource DefaultControlHeight}" />
<Setter Property="Padding"
Value="10,0" />
<Setter Property="Template">
<ControlTemplate TargetType="Button">
<Border x:Name="BorderRoot"
CornerRadius="{TemplateBinding CornerRadius}"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}">
<Path x:Name="PathMain"
Width="{TemplateBinding hc:IconElement.Width}"
Height="{TemplateBinding hc:IconElement.Height}"
Fill="{TemplateBinding Foreground}"
IsVisible="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNotNull}}"
Stretch="Uniform"
Data="{TemplateBinding hc:IconElement.Geometry}" />
<ContentPresenter x:Name="contentPresenter"
RecognizesAccessKey="True"
VerticalAlignment="Center"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
IsVisible="{Binding $self.Content, Converter={x:Static ObjectConverters.IsNotNull}}"
Margin="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometrySpacingConverter}, ConverterParameter='6,0,0,0'}" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^:pointerover">
<Setter Property="Background"
Value="{DynamicResource SecondaryRegionBrush}" />
<Style Selector="^ /template/ ContentPresenter#contentPresenter">
<Setter Property="Foreground"
Value="{DynamicResource PrimaryBrush}" />
</Style>
</Style>
<Style Selector="^:pressed">
<Setter Property="Background"
Value="{DynamicResource BorderBrush}" />
</Style>
<Style Selector="^:disabled">
<Setter Property="Opacity"
Value="0.4" />
</Style>
</ControlTheme>
<ControlTheme x:Key="ButtonGroupItemDefault"
BasedOn="{StaticResource ButtonGroupItemBaseStyle}"
TargetType="Button" />
<ControlTheme x:Key="ButtonGroupItemHorizontalFirst"
BasedOn="{StaticResource ButtonGroupItemBaseStyle}"
TargetType="Button">
<Setter Property="hc:BorderElement.CornerRadius"
Value="4,0,0,4" />
</ControlTheme>
<ControlTheme x:Key="ButtonGroupItemHorizontalLast"
BasedOn="{StaticResource ButtonGroupItemBaseStyle}"
TargetType="Button">
<Setter Property="hc:BorderElement.CornerRadius"
Value="0,4,4,0" />
</ControlTheme>
<ControlTheme x:Key="ButtonGroupItemSingle"
BasedOn="{StaticResource ButtonGroupItemBaseStyle}"
TargetType="Button">
<Setter Property="hc:BorderElement.CornerRadius"
Value="{StaticResource DefaultCornerRadius}" />
</ControlTheme>
<ControlTheme x:Key="ButtonGroupItemVerticalFirst"
BasedOn="{StaticResource ButtonGroupItemBaseStyle}"
TargetType="Button">
<Setter Property="hc:BorderElement.CornerRadius"
Value="4,4,0,0" />
</ControlTheme>
<ControlTheme x:Key="ButtonGroupItemVerticalLast"
BasedOn="{StaticResource ButtonGroupItemBaseStyle}"
TargetType="Button">
<Setter Property="hc:BorderElement.CornerRadius"
Value="0,0,4,4" />
</ControlTheme>
<ControlTheme x:Key="ButtonCustom"
TargetType="Button">
<Setter Property="Foreground"
@@ -0,0 +1,67 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:ClassModifier="internal"
xmlns:hc="clr-namespace:HandyControl.Controls">
<ItemsPanelTemplate x:Key="ButtonGroupHorizontalUniformItemsPanelTemplate">
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="ButtonGroupVerticalUniformItemsPanelTemplate">
<UniformGrid Columns="1" />
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="ButtonGroupHorizontalStackItemsPanelTemplate">
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="ButtonGroupVerticalStackItemsPanelTemplate">
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
<ControlTheme x:Key="ButtonGroupBaseStyle"
TargetType="hc:ButtonGroup">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Layout" Value="Uniform" />
<Setter Property="Focusable" Value="False" />
<Setter Property="ItemsPanel" Value="{StaticResource ButtonGroupHorizontalUniformItemsPanelTemplate}" />
<Setter Property="hc:VisualElement.HighlightBorderBrush" Value="{DynamicResource PrimaryBrush}" />
<Setter Property="hc:VisualElement.HighlightBackground" Value="{DynamicResource RegionBrush}" />
<Setter Property="hc:VisualElement.HighlightForeground" Value="{DynamicResource PrimaryBrush}" />
<Setter Property="Template">
<ControlTemplate TargetType="hc:ButtonGroup">
<Border Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}">
<ItemsPresenter Name="PART_ItemsPresenter"
ItemsPanel="{TemplateBinding ItemsPanel}" />
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^[Layout=Uniform][Orientation=Vertical]">
<Setter Property="ItemsPanel" Value="{StaticResource ButtonGroupVerticalUniformItemsPanelTemplate}" />
</Style>
<Style Selector="^[Layout=Stack][Orientation=Horizontal]">
<Setter Property="ItemsPanel" Value="{StaticResource ButtonGroupHorizontalStackItemsPanelTemplate}" />
</Style>
<Style Selector="^[Layout=Stack][Orientation=Vertical]">
<Setter Property="ItemsPanel" Value="{StaticResource ButtonGroupVerticalStackItemsPanelTemplate}" />
</Style>
</ControlTheme>
<ControlTheme x:Key="{x:Type hc:ButtonGroup}"
BasedOn="{StaticResource ButtonGroupBaseStyle}"
TargetType="hc:ButtonGroup" />
<ControlTheme x:Key="ButtonGroupSolid"
BasedOn="{StaticResource ButtonGroupBaseStyle}"
TargetType="hc:ButtonGroup">
<Setter Property="hc:VisualElement.HighlightBorderBrush" Value="{DynamicResource TitleBrush}" />
<Setter Property="hc:VisualElement.HighlightBackground" Value="{DynamicResource TitleBrush}" />
<Setter Property="hc:VisualElement.HighlightForeground" Value="{DynamicResource TextIconBrush}" />
</ControlTheme>
</ResourceDictionary>
@@ -59,6 +59,7 @@
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/HeaderedContentControl.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/Card.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/ElementGroup.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/ButtonGroup.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Styles.Resources>
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:ClassModifier="internal"
xmlns:hc="clr-namespace:HandyControl.Controls">
@@ -215,4 +215,134 @@
<Setter Property="hc:IconElement.Height"
Value="12" />
</ControlTheme>
<ControlTheme x:Key="RadioGroupItemBaseStyle"
TargetType="RadioButton">
<Setter Property="Background"
Value="{DynamicResource RegionBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource BorderBrush}" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="VerticalAlignment"
Value="Center" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
<Setter Property="hc:BorderElement.CornerRadius"
Value="0" />
<Setter Property="CornerRadius"
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
<Setter Property="Height"
Value="{StaticResource DefaultControlHeight}" />
<Setter Property="Padding"
Value="10,0" />
<Setter Property="hc:IconElement.Width"
Value="16" />
<Setter Property="hc:IconElement.Height"
Value="16" />
<Setter Property="Template">
<ControlTemplate TargetType="RadioButton">
<Border x:Name="BorderRoot"
CornerRadius="{TemplateBinding CornerRadius}"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}">
<Path x:Name="PathMain"
Width="{TemplateBinding hc:IconElement.Width}"
Height="{TemplateBinding hc:IconElement.Height}"
Fill="{Binding $parent[RadioButton].Foreground}"
IsVisible="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNotNull}}"
Stretch="Uniform"
Data="{TemplateBinding hc:IconElement.Geometry}" />
<ContentPresenter x:Name="contentPresenter"
RecognizesAccessKey="True"
VerticalAlignment="Center"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
IsVisible="{Binding $self.Content, Converter={x:Static ObjectConverters.IsNotNull}}"
Margin="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometrySpacingConverter}, ConverterParameter='6,0,0,0'}" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^:pointerover">
<Setter Property="Background"
Value="{DynamicResource SecondaryRegionBrush}" />
<Style Selector="^ /template/ ContentPresenter#contentPresenter">
<Setter Property="Foreground"
Value="{DynamicResource PrimaryBrush}" />
</Style>
</Style>
<Style Selector="^:pressed">
<Setter Property="Background"
Value="{DynamicResource BorderBrush}" />
</Style>
<Style Selector="^:checked">
<Setter Property="ZIndex"
Value="2147483647" />
<Style Selector="^ /template/ Border#BorderRoot">
<Setter Property="BorderBrush"
Value="{Binding $parent[RadioButton].(hc:VisualElement.HighlightBorderBrush)}" />
<Setter Property="Background"
Value="{Binding $parent[RadioButton].(hc:VisualElement.HighlightBackground)}" />
</Style>
<Style Selector="^ /template/ ContentPresenter#contentPresenter">
<Setter Property="Foreground"
Value="{Binding $parent[RadioButton].(hc:VisualElement.HighlightForeground)}" />
</Style>
</Style>
<Style Selector="^:disabled">
<Setter Property="Opacity"
Value="0.4" />
</Style>
</ControlTheme>
<ControlTheme x:Key="RadioGroupItemDefault"
BasedOn="{StaticResource RadioGroupItemBaseStyle}"
TargetType="RadioButton" />
<ControlTheme x:Key="RadioGroupItemHorizontalFirst"
BasedOn="{StaticResource RadioGroupItemBaseStyle}"
TargetType="RadioButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="4,0,0,4" />
</ControlTheme>
<ControlTheme x:Key="RadioGroupItemHorizontalLast"
BasedOn="{StaticResource RadioGroupItemBaseStyle}"
TargetType="RadioButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="0,4,4,0" />
</ControlTheme>
<ControlTheme x:Key="RadioGroupItemSingle"
BasedOn="{StaticResource RadioGroupItemBaseStyle}"
TargetType="RadioButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="{StaticResource DefaultCornerRadius}" />
</ControlTheme>
<ControlTheme x:Key="RadioGroupItemVerticalFirst"
BasedOn="{StaticResource RadioGroupItemBaseStyle}"
TargetType="RadioButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="4,4,0,0" />
</ControlTheme>
<ControlTheme x:Key="RadioGroupItemVerticalLast"
BasedOn="{StaticResource RadioGroupItemBaseStyle}"
TargetType="RadioButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="0,0,4,4" />
</ControlTheme>
</ResourceDictionary>
@@ -769,4 +769,130 @@
<Setter Property="Width"
Value="20" />
</ControlTheme>
<ControlTheme x:Key="ToggleButtonGroupItemBaseStyle"
TargetType="ToggleButton">
<Setter Property="Background"
Value="{DynamicResource RegionBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource BorderBrush}" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="VerticalAlignment"
Value="Center" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
<Setter Property="hc:BorderElement.CornerRadius"
Value="0" />
<Setter Property="CornerRadius"
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
<Setter Property="Height"
Value="{StaticResource DefaultControlHeight}" />
<Setter Property="Padding"
Value="10,0" />
<Setter Property="Template">
<ControlTemplate TargetType="ToggleButton">
<Border x:Name="BorderRoot"
CornerRadius="{TemplateBinding CornerRadius}"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}">
<Path x:Name="PathMain"
Width="{TemplateBinding hc:IconElement.Width}"
Height="{TemplateBinding hc:IconElement.Height}"
Fill="{TemplateBinding Foreground}"
IsVisible="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNotNull}}"
Stretch="Uniform"
Data="{TemplateBinding hc:IconElement.Geometry}" />
<ContentPresenter x:Name="contentPresenter"
RecognizesAccessKey="True"
VerticalAlignment="Center"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
IsVisible="{Binding $self.Content, Converter={x:Static ObjectConverters.IsNotNull}}"
Margin="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometrySpacingConverter}, ConverterParameter='6,0,0,0'}" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^:pointerover">
<Setter Property="Background"
Value="{DynamicResource SecondaryRegionBrush}" />
<Style Selector="^ /template/ ContentPresenter#contentPresenter">
<Setter Property="Foreground"
Value="{DynamicResource PrimaryBrush}" />
</Style>
</Style>
<Style Selector="^:pressed">
<Setter Property="Background"
Value="{DynamicResource BorderBrush}" />
</Style>
<Style Selector="^:checked">
<Setter Property="ZIndex"
Value="2147483647" />
<Style Selector="^ /template/ Border#BorderRoot">
<Setter Property="BorderBrush"
Value="{Binding $parent[ToggleButton].(hc:VisualElement.HighlightBorderBrush)}" />
<Setter Property="Background"
Value="{Binding $parent[ToggleButton].(hc:VisualElement.HighlightBackground)}" />
</Style>
<Style Selector="^ /template/ ContentPresenter#contentPresenter">
<Setter Property="Foreground"
Value="{Binding $parent[ToggleButton].(hc:VisualElement.HighlightForeground)}" />
</Style>
</Style>
<Style Selector="^:disabled">
<Setter Property="Opacity"
Value="0.4" />
</Style>
</ControlTheme>
<ControlTheme x:Key="ToggleButtonGroupItemDefault"
BasedOn="{StaticResource ToggleButtonGroupItemBaseStyle}"
TargetType="ToggleButton" />
<ControlTheme x:Key="ToggleButtonGroupItemHorizontalFirst"
BasedOn="{StaticResource ToggleButtonGroupItemBaseStyle}"
TargetType="ToggleButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="4,0,0,4" />
</ControlTheme>
<ControlTheme x:Key="ToggleButtonGroupItemHorizontalLast"
BasedOn="{StaticResource ToggleButtonGroupItemBaseStyle}"
TargetType="ToggleButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="0,4,4,0" />
</ControlTheme>
<ControlTheme x:Key="ToggleButtonGroupItemSingle"
BasedOn="{StaticResource ToggleButtonGroupItemBaseStyle}"
TargetType="ToggleButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="{StaticResource DefaultCornerRadius}" />
</ControlTheme>
<ControlTheme x:Key="ToggleButtonGroupItemVerticalFirst"
BasedOn="{StaticResource ToggleButtonGroupItemBaseStyle}"
TargetType="ToggleButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="4,4,0,0" />
</ControlTheme>
<ControlTheme x:Key="ToggleButtonGroupItemVerticalLast"
BasedOn="{StaticResource ToggleButtonGroupItemBaseStyle}"
TargetType="ToggleButton">
<Setter Property="hc:BorderElement.CornerRadius"
Value="0,0,4,4" />
</ControlTheme>
</ResourceDictionary>