WPF CheckBox 样式

Posted 多放辣椒

tags:

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

 

 

<Style x:Key="FocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <SolidColorBrush x:Key="OptionMark.Static.Background" Color="#FFFFFFFF"/>
    <SolidColorBrush x:Key="OptionMark.Static.Border" Color="#FF707070"/>
    <Style x:Key="OptionMarkFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Background" Color="#FFF3F9FF"/>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Border" Color="#FF5593FF"/>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Glyph" Color="#FF212121"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Background" Color="#FFE6E6E6"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Border" Color="#FFBCBCBC"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Glyph" Color="#FF707070"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Background" Color="#FFD9ECFF"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Border" Color="#FF3C77DD"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Glyph" Color="#FF212121"/>
    <SolidColorBrush x:Key="OptionMark.Static.Glyph" Color="#FF212121"/>

    <Style x:Key="myCheckStyle" TargetType="{x:Type CheckBox}">
        <Setter Property="Effect">
            <Setter.Value>
                <DropShadowEffect ShadowDepth="0"  
    				Color="Gray"  
    				Opacity="1"  
    				BlurRadius="20"/>
            </Setter.Value>
        </Setter>
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource OptionMark.Static.Background}"/>
        <Setter Property="BorderBrush" Value="{StaticResource OptionMark.Static.Border}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">

                    <Viewbox x:Name="viewbox" Stretch="Uniform">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CheckStates">

                                <VisualState x:Name="Indeterminate"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard RepeatBehavior="Forever" AutoReverse="True">
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="ellipse">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:5" Value="180"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ellipse">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                                            <DiscreteObjectKeyFrame KeyTime="0:0:5" Value="{x:Static Visibility.Visible}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid Width="75" Height="75" x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
                            <Ellipse x:Name="ellipse"   Visibility="Hidden"  Height="75" Width="75" Stroke="Green" StrokeDashArray="4 4" StrokeThickness="3" RenderTransformOrigin="0.5,0.5">
                                <Ellipse.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Ellipse.RenderTransform>
                            </Ellipse>
                            <Border x:Name="border1" Height="70" Width="70" CornerRadius="35" BorderThickness="1" Background="White" BorderBrush="LightGray" >

                                <Border x:Name="border" Height="56" Width="56" CornerRadius="28" BorderThickness="0" BorderBrush="{TemplateBinding BorderBrush}"  Background="{TemplateBinding Background}" SnapsToDevicePixels="true">

                                    <ContentPresenter Margin="6,5"  x:Name="contentPresenter"   Focusable="False" HorizontalAlignment="Center"  RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                                </Border>
                            </Border>
                        </Grid>
                    </Viewbox>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsChecked" Value="True">

                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                        <GradientStop Color="#15b9b9" Offset="0"/>
                                        <GradientStop Color="#1e6b87" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>

                        </Trigger>
                        <Trigger Property="IsChecked" Value="False">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                        <GradientStop Color="#5a5e6b" Offset="0"/>
                                        <GradientStop Color="#a6abbd" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>

                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

  

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

WPF 自定义CheckBox样式

wpf ListBox的CheckBox样式设置

WPF CheckBox 样式

wpf datagrid中添加一列checkbox,反复点选,会出现checkbox框偏移,请wpf大神帮忙!

wpf combobox样式不显示倒三角

WPF 动态添加控件以及样式字典的引用(Style introduction)