WPF 样式设计之——radioButton

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 样式设计之——radioButton相关的知识,希望对你有一定的参考价值。

如何在WPF的cs文件中定义图片RadioButton呢?
把图片加到了resource文件里面,希望在窗口中显示图片radiobutton,请问这样的radiobutton应该怎么定义呢?以下为处理的样式:

XML code

<RadioButton.Style>
    <Style TargetType="{x:Type RadioButton}">
        <Style.Resources>
            <Style x:Key="CheckRadioFocusVisual">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Rectangle
                                Margin="14,0,0,0"
                                StrokeThickness="1"
                                Stroke="Black"
                                StrokeDashArray="1 2"
                                SnapsToDevicePixels="true"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="#dddddd"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <BulletDecorator Background="Transparent">
                        <BulletDecorator.Bullet>
                            <!-- 这里是修改过的部分 开始 -->
                            <Border
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                <Image x:Name="PART_Image" Width="12">
                                    <Image.Source>
                                        <DrawingImage>
                                            <DrawingImage.Drawing>
                                                <GeometryDrawing Brush="OrangeRed" Geometry="M 0 0 L 3.5 4 L 7 0 Z"/>
                                            </DrawingImage.Drawing>
                                        </DrawingImage>
                                    </Image.Source>
                                    <Image.Effect>
                                        <DropShadowEffect BlurRadius="1" Opacity="0.75" ShadowDepth="0"/>
                                    </Image.Effect>
                                </Image>
                            </Border>
                            <!-- 这里是修改过的部分 结束 -->
                        </BulletDecorator.Bullet>
                        <ContentPresenter
                            Margin="{TemplateBinding Padding}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            RecognizesAccessKey="True"/>
                    </BulletDecorator>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasContent"
                                    Value="true">
                            <Setter Property="FocusVisualStyle"
                                    Value="{StaticResource CheckRadioFocusVisual}"/>
                            <Setter Property="Padding"
                                    Value="4,0,0,0"/>
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                    Value="false">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                        <!-- 自定义触发器 开始 -->
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="PART_Image" Property="Source">
                                <Setter.Value>
                                    <DrawingImage>
                                        <DrawingImage.Drawing>
                                            <GeometryDrawing Brush="OrangeRed" Geometry="M 0,4 L 3.5,0 L 7,4 Z"/>
                                        </DrawingImage.Drawing>
                                    </DrawingImage>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <!-- 自定义触发器 结束 -->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</RadioButton.Style>

以上是关于WPF 样式设计之——radioButton的主要内容,如果未能解决你的问题,请参考以下文章

wpf radiobuttong 去前面的圆点, 自定义radiobutton样式

WPF 让一组 Button 实现 RadioButton 的当前样式效果

WPF RadioButton/ToggleButton 样式

WPF 基础控件之 TabControl样式

WPF 基础控件之 Slider 样式

wpf 如何将radiobutton 封装成imagebutton控件 主要是样式怎么改?麻烦说的详细一些。谢谢~