首先看下效果。
选中:
不选中
样式:
<Style x:Key="CheckStyle" TargetType="{x:Type CheckBox}"> <Setter Property="Height" Value="18"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type CheckBox}"> <Border x:Name="ForegroundPanel" CornerRadius="2" BorderBrush="Black" BorderThickness="1" Padding="0"> <DockPanel> <TextBlock x:Name="Content" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="2,0" /> <Border x:Name="CheckFlag" HorizontalAlignment="Right" VerticalAlignment="Center" CornerRadius="2" BorderThickness="0" Width="18" Height="16"/> </DockPanel> </Border> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="Content" Property="Text" Value="是"/> <Setter TargetName="ForegroundPanel" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Gray" Offset="0.0"/> <GradientStop Color="Blue" Offset="0.5"/> <GradientStop Color="Gray" Offset="1"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter TargetName="CheckFlag" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="WhiteSmoke" Offset="0.0"/> <GradientStop Color="Green" Offset="0.5"/> <GradientStop Color="WhiteSmoke" Offset="1"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter TargetName="Content" Property="DockPanel.Dock" Value="Left"/> <Setter TargetName="CheckFlag" Property="DockPanel.Dock" Value="Right"/> </Trigger> <Trigger Property="IsChecked" Value="False"> <Setter TargetName="Content" Property="Text" Value="否"/> <Setter TargetName="ForegroundPanel" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Gray" Offset="0.0"/> <GradientStop Color="Silver" Offset="0.5"/> <GradientStop Color="Gray" Offset="1"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter TargetName="CheckFlag" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="WhiteSmoke" Offset="0.0"/> <GradientStop Color="Silver" Offset="0.5"/> <GradientStop Color="WhiteSmoke" Offset="1"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter TargetName="Content" Property="DockPanel.Dock" Value="Right"/> <Setter TargetName="CheckFlag" Property="DockPanel.Dock" Value="Left"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
在程序中的使用:
<CheckBox Content="CheckBox" Height="34" HorizontalAlignment="Left" Margin="140,162,0,0" Name="checkBox1" VerticalAlignment="Top" Width="151" Style="{StaticResource CheckStyle}"/>
代码下载: http://download.csdn.net/detail/yysyangyangyangshan/5647793