如何将WinUI 3 RadioButtons组样式设置为切换按钮组?
在WinUI中将单选按钮设置为切换按钮样式的实现方法
效果示例

现有代码
<StackPanel > <RadioButtons Header="Options:" MaxColumns="3"> <RadioButton Content="Option 1" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option1RadioButton"/> <RadioButton Content="Option 2" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option2RadioButton"/> <RadioButton Content="Option 3" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option3RadioButton"/> </RadioButtons> </StackPanel>
实现步骤
1. 定义RadioButton的切换按钮样式
在页面的Resources或全局资源字典中添加以下自定义样式,该样式模仿WinUI内置ToggleButton的视觉效果,同时保留RadioButton的互斥选择逻辑:
<Style x:Key="ToggleStyleRadioButton" TargetType="RadioButton"> <Setter Property="Background" Value="{ThemeResource ToggleButtonBackground}"/> <Setter Property="Foreground" Value="{ThemeResource ToggleButtonForeground}"/> <Setter Property="BorderBrush" Value="{ThemeResource ToggleButtonBorderBrush}"/> <Setter Property="BorderThickness" Value="{ThemeResource ToggleButtonBorderThemeThickness}"/> <Setter Property="Padding" Value="8,6"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/> <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/> <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="RadioButton"> <Grid x:Name="RootGrid" Background="{TemplateBinding Background}" CornerRadius="{ThemeResource ToggleButtonCornerRadius}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="PointerOver"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonBackgroundPointerOver}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonForegroundPointerOver}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonBorderBrushPointerOver}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonBackgroundPressed}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonForegroundPressed}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonBorderBrushPressed}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Disabled"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonBackgroundDisabled}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonForegroundDisabled}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonBorderBrushDisabled}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:Name="CheckStates"> <VisualState x:Name="Checked"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonBackgroundChecked}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonForegroundChecked}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonBorderBrushChecked}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Unchecked"/> <VisualState x:Name="Indeterminate"/> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ContentPresenter x:Name="ContentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" AutomationProperties.AccessibilityView="Raw"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
2. 应用样式到RadioButton
修改原有代码,为每个RadioButton指定上述自定义样式:
<StackPanel> <RadioButtons Header="Options:" MaxColumns="3"> <RadioButton Content="Option 1" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option1RadioButton" Style="{StaticResource ToggleStyleRadioButton}"/> <RadioButton Content="Option 2" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option2RadioButton" Style="{StaticResource ToggleStyleRadioButton}"/> <RadioButton Content="Option 3" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option3RadioButton" Style="{StaticResource ToggleStyleRadioButton}"/> </RadioButtons> </StackPanel>
说明
该样式复用WinUI内置的ToggleButton主题资源,确保与系统主题风格一致,同时完整保留RadioButton的互斥选择特性,IsEnabled状态也会正确应用对应的视觉样式,完全匹配示例图的切换按钮效果。
内容的提问来源于stack exchange,提问作者Ming
相关产品推荐
相关产品推荐

