首页 > 代码库 > WPF CheckBox 样式

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="http://www.mamicode.com/{StaticResource FocusVisual}"/>
        <Setter Property="Background" Value="http://www.mamicode.com/{StaticResource OptionMark.Static.Background}"/>
        <Setter Property="BorderBrush" Value="http://www.mamicode.com/{StaticResource OptionMark.Static.Border}"/>
        <Setter Property="Foreground" Value="http://www.mamicode.com/{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="http://www.mamicode.com/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="http://www.mamicode.com/0"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:5" Value="http://www.mamicode.com/180"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ellipse">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="http://www.mamicode.com/{x:Static Visibility.Visible}"/>
                                            <DiscreteObjectKeyFrame KeyTime="0:0:5" Value="http://www.mamicode.com/{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="http://www.mamicode.com/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="http://www.mamicode.com/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 样式