WPF UI框架界面开发教程(十二)

92 篇文章 9 订阅
92 篇文章 24 订阅

为了配合界面统一风格,在样式中添加单选按钮,RadioButton选中后,Background变成蓝色,圆角给个1000。

在样式中先添加两列,第一列显示是否选中单选按钮,而且给的高度和宽度都是20,所以显示是圆形,第二列是显示文本内容,演示看下RadioButton运行后效果。

先不添加依赖属性,只给默认样式代码:

<Style x:Key="FocusVisualStyle">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="-2" SnapsToDevicePixels="True"
                               StrokeThickness="1" StrokeDashArray="1 2"
                               Stroke="{StaticResource WordBlueBrush}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type RadioButton}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualStyle}" />
        <Setter Property="Background" Value="{StaticResource BackgroundLightBrush}" />
        <Setter Property="BorderBrush" Value="{StaticResource WordBlueBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource ForegroundMainBrush}"/>
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="FontSize" Value="20" />
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Border x:Name="radioButtonBorder"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}"
                                CornerRadius="1000"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                Margin="1,1,5,1"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                Effect="{TemplateBinding Effect}"
                                Width="20"
                                Height="20">
                            <Border x:Name="border" Margin="2" Background="Transparent" CornerRadius="1000">
                            </Border>
                        </Border>
                        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                          Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Background" TargetName="border" Value="{StaticResource WordBlueBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.5" />
            </Trigger>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="BorderBrush" Value="{StaticResource WordBlueBrush}" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="BorderBrush" Value="{StaticResource WordBlueBrush}" />
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="BorderBrush" Value="{StaticResource WordBlueBrush}" />
            </Trigger>
        </Style.Triggers>
    </Style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值