Checkbox template and style, Checkbox template Style
1 <Style TargetType="{x:Type CheckBox}"> 2 <Setter Property="FontFamily" Value="Microsoft YaHei" /> 3 <Setter Property="FontSize" Value="12" /> 4 <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 5 <Setter Property="Background" Value="{DynamicResource ButtonBackground}" /> 6 <Setter Property="BorderBrush" Value="{DynamicResource ButtonBorder}" /> 7 <Setter Property="Foreground" Value="{DynamicResource WindowText}" /> 8 <Setter Property="BorderThickness" Value="1" /> 9 <Setter Property="Template">10 <Setter.Value>11 <ControlTemplate TargetType="{x:Type CheckBox}">12 <Grid x:Name="LayoutRoot" Margin="{TemplateBinding Padding}">13 <Grid.ColumnDefinitions>14 <ColumnDefinition Width="Auto" />15 <ColumnDefinition Width="*" />16 </Grid.ColumnDefinitions>17 <Border x:Name="CheckBorder" Width="16" Height="16" Margin="0,1,4,0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">18 <Grid>19 <Path x:Name="CheckMark"20 Width="10" Height="10"21 Margin="2"22 Data="F1M15,21L22,28 32.9999,14.0001 35.9999,17.0001 22,34 12.0001,24 15,21z"23 Fill="{TemplateBinding Foreground}"24 Stretch="Fill" Visibility="Collapsed" />25 <Rectangle x:Name="Indeterminate" Width="8" Height="8" Margin="4" Fill="{TemplateBinding Foreground}" Visibility="Hidden" />26 </Grid>27 </Border>28 <ContentPresenter Grid.Column="1"29 Margin="{TemplateBinding Padding}"30 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"31 VerticalAlignment="Center"32 Content="{TemplateBinding Content}"33 ContentStringFormat="{TemplateBinding ContentStringFormat}"34 ContentTemplate="{TemplateBinding ContentTemplate}"35 Focusable="False" RecognizesAccessKey="True"36 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />37 </Grid>38 <ControlTemplate.Triggers>39 <MultiDataTrigger>40 <MultiDataTrigger.Conditions>41 <Condition Binding="{Binding IsThreeState, RelativeSource={RelativeSource Self}}" Value="True" />42 <Condition Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="True" />43 </MultiDataTrigger.Conditions>44 <Setter Property="Foreground" Value="{DynamicResource ButtonTextDisabled}" />45 <Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed" />46 <Setter TargetName="Indeterminate" Property="Visibility" Value="Visible" />47 </MultiDataTrigger>48 <Trigger Property="IsEnabled" Value="False">49 <Setter Property="Foreground" Value="{DynamicResource ButtonTextDisabled}" />50 </Trigger>51 <Trigger Property="IsChecked" Value="True">52 <Setter TargetName="CheckMark" Property="Visibility" Value="Visible" />53 <Setter TargetName="Indeterminate" Property="Visibility" Value="Collapsed" />54 </Trigger>55 <Trigger Property="IsMouseOver" Value="True">56 <Setter TargetName="CheckBorder" Property="Background" Value="{DynamicResource ButtonBackgroundHover}" />57 <Setter TargetName="CheckBorder" Property="BorderBrush" Value="{DynamicResource ButtonBorderHover}" />58 <Setter TargetName="CheckMark" Property="Fill" Value="{DynamicResource ButtonTextHover}" />59 </Trigger>60 <Trigger Property="IsPressed" Value="True">61 <Setter TargetName="CheckBorder" Property="Background" Value="{DynamicResource ButtonBackgroundPressed}" />62 <Setter TargetName="CheckBorder" Property="BorderBrush" Value="{DynamicResource ButtonBorderPressed}" />63 <Setter TargetName="CheckMark" Property="Fill" Value="{DynamicResource ButtonTextPressed}" />64 </Trigger>65 <Trigger Property="IsFocused" Value="True">66 <Setter TargetName="CheckBorder" Property="BorderBrush" Value="{DynamicResource Accent}" />67 </Trigger>68 </ControlTemplate.Triggers>69 </ControlTemplate>70 </Setter.Value>71 </Setter>72 </Style>
Application
1 <CheckBox Content="checkbox"/>
Reference:
Https://msdn.microsoft.com/zh-cn/library/ms752319 (v = vs.110). aspx
Some questions will be added later