You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI 9:如何根据RadioButton选中状态动态修改文本颜色

解决.NET MAUI RadioButton自定义外观的问题

要实现隐藏默认勾选标记、根据选中状态切换文本颜色和背景色的效果,你需要调整ControlTemplate结构,并通过VisualState直接控制文本元素的属性。以下是修改后的完整样式:

<Style TargetType="RadioButton" x:Key="WizardRadioButton">
    <Setter Property="FontFamily" Value="Inter-Bold"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="Margin" Value="0,6,0,6"/>
    <Setter Property="MinimumHeightRequest" Value="44"/>
    <Setter Property="MinimumWidthRequest" Value="44"/>
    <!-- 默认未选中时的文本颜色,适配明暗主题 -->
    <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource White}}"/>
    <Setter Property="BackgroundColor" Value="Transparent"/>
    <Setter Property="ControlTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Border x:Name="RadioBorder"
                        StrokeThickness="0"
                        Stroke="Transparent"
                        Padding="10,5"
                        BackgroundColor="Transparent"
                        StrokeShape="RoundRectangle 8">
                    <!-- 给ContentPresenter命名,方便在VisualState中控制文本颜色 -->
                    <ContentPresenter x:Name="RadioContent"
                                      VerticalOptions="Center"
                                      HorizontalOptions="Center"
                                      TextColor="{TemplateBinding TextColor}"/>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CheckedStates">
                            <VisualState x:Name="Checked">
                                <VisualState.Setters>
                                    <!-- 选中时的背景色 -->
                                    <Setter TargetName="RadioBorder" Property="BackgroundColor" Value="#1CB41C"/>
                                    <!-- 选中时的文本颜色 -->
                                    <Setter TargetName="RadioContent" Property="TextColor" Value="White"/>
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Unchecked">
                                <VisualState.Setters>
                                    <!-- 未选中时的背景色 -->
                                    <Setter TargetName="RadioBorder" Property="BackgroundColor" Value="White"/>
                                    <!-- 未选中时继承RadioButton的默认TextColor(适配主题) -->
                                    <Setter TargetName="RadioContent" Property="TextColor" Value="{TemplateBinding TextColor}"/>
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

关键修改说明:

  • 隐藏默认勾选标记:自定义ControlTemplate完全替换了RadioButton的默认视觉结构,只保留Border和ContentPresenter,未包含原生勾选图标元素,自然隐藏了默认标记。
  • 动态文本颜色控制:给ContentPresenter命名为RadioContent,在VisualState中通过TargetName直接设置其TextColor属性——选中时设为白色,未选中时绑定回RadioButton的默认TextColor(适配明暗主题)。
  • 背景色切换:通过TargetName="RadioBorder"精准控制容器背景色,确保选中/未选中状态的视觉区分。

使用样式时直接给RadioButton应用即可:

<RadioButton Style="{StaticResource WizardRadioButton}" Content="选项1"/>
<RadioButton Style="{StaticResource WizardRadioButton}" Content="选项2"/>

内容的提问来源于stack exchange,提问作者Felix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 09:56:19