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

WPF C#中RadioButton按钮与文本高度不对齐的解决方法

WPF RadioButton按钮与文本垂直对齐问题的解决方法

在WPF中使用大字号的RadioButton时,常出现按钮图标与文本垂直不对齐的问题,这是默认控件模板的布局未适配字号变化导致的。以下是几种可行的解决方式:

快速临时修复

直接调整RadioButton的VerticalContentAlignment和Padding属性,根据实际显示效果微调数值:

<RadioButton Content="Save in Custom Folder" 
             IsChecked="{Binding IsSaveInCustomFolder}" 
             FontSize="16" 
             VerticalAlignment="Center"
             VerticalContentAlignment="Center"
             Padding="0,0,0,2"/> <!-- 底部Padding可根据字号大小调整 -->

彻底解决:自定义ControlTemplate

如果需要全局适配不同字号的RadioButton,推荐自定义控件模板,确保按钮与文本始终垂直居中对齐:

<Style TargetType="RadioButton">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="#FF000000"/>
    <Setter Property="Foreground" Value="#FF000000"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RadioButton">
                <Grid VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <!-- 单选按钮核心控件 -->
                    <Grid Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center">
                        <Ellipse x:Name="Bullet" 
                                 Width="14" 
                                 Height="14" 
                                 Stroke="{TemplateBinding BorderBrush}" 
                                 StrokeThickness="2"
                                 Fill="{TemplateBinding Background}"/>
                        <Ellipse x:Name="CheckMark" 
                                 Width="8" 
                                 Height="8" 
                                 Fill="{TemplateBinding Foreground}"
                                 Visibility="Collapsed"/>
                    </Grid>
                    <!-- 文本内容 -->
                    <ContentPresenter Grid.Column="1" 
                                      Margin="6,0,0,0" 
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="CheckMark" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Bullet" Property="Opacity" Value="0.5"/>
                        <Setter TargetName="CheckMark" Property="Opacity" Value="0.5"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

将上述样式添加到资源字典或窗口资源中,所有RadioButton会自动应用该模板,按钮与文本的垂直对齐问题将被彻底解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 12:10:20