【问题标题】:How to put text on top in RadioButton如何将文本放在 RadioButton 的顶部
【发布时间】:2014-07-09 19:43:26
【问题描述】:

我正在尝试实现附件中显示的效果,但没有成功。甚至有可能吗?我试图将文本框放在单选按钮内并将其设置为水平和垂直内容对齐,但它没有按我想要的方式工作。 欢迎任何建议。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    资源

    <Window.Resources>
        <Style x:Key="RadioButton" TargetType="RadioButton">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <StackPanel>
                            <TextBlock FontFamily="Segoe Ui Dark" HorizontalAlignment="Center"  Foreground="Black" Text="{TemplateBinding Content}" FontSize="12" FontWeight="SemiBold" Margin="0,0,0,2" />
                            <BulletDecorator Grid.Column="0" Background="Transparent" >
                                <BulletDecorator.Bullet>
                                    <Grid Width="14" Height="14" >
                                        <Ellipse x:Name="Border" Fill="Transparent" StrokeThickness="1" Stroke="Black" />
                                        <Ellipse x:Name="CheckMark" Margin="4" Fill="Black"/>
                                    </Grid>
                                </BulletDecorator.Bullet>
                            </BulletDecorator>
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="false">
                                <Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter TargetName="Border" Property="Fill" Value="LightGray" />
                                <Setter TargetName="Border" Property="Stroke" Value="Black" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter TargetName="Border" Property="Fill" Value="White" />
                                <Setter TargetName="Border" Property="Stroke" Value="Black" />
                                <Setter TargetName="Border" Property="StrokeThickness" Value="4" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
    
        </Style>
    </Window.Resources>
    

    Xaml

    <StackPanel Orientation="Horizontal">
        <RadioButton Content="1" Style="{StaticResource RadioButton}" Margin="5"></RadioButton>
        <RadioButton Content="2" Style="{StaticResource RadioButton}" Margin="5"></RadioButton>
        <RadioButton Content="3" Style="{StaticResource RadioButton}" Margin="5"></RadioButton>
        <RadioButton Content="4" Style="{StaticResource RadioButton}" Margin="5" ></RadioButton>
        <RadioButton Content="5" Style="{StaticResource RadioButton}" Margin="5"></RadioButton>
    </StackPanel>
    

    结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 2020-11-16
      • 2021-12-23
      • 1970-01-01
      • 2010-10-06
      相关资源
      最近更新 更多