Customized CheckBox : CheckBox « Windows Presentation Foundation « C# / CSharp Tutorial






<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <ControlTemplate x:Key="switch" TargetType="{x:Type CheckBox}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Canvas Background="LightGray">
                    <Line Name="lineOff"
                              StrokeThickness="8" Stroke="Black"
                              X1="48" Y1="40" X2="20" Y2="16"
                              StrokeStartLineCap="Round"
                              StrokeEndLineCap="Round"  />
                    <Line Name="lineOn"
                              StrokeThickness="8" Stroke="Black"
                              X1="48" Y1="40" X2="76" Y2="16"
                              StrokeStartLineCap="Round"
                              StrokeEndLineCap="Round" 
                              Visibility="Hidden"  />
                </Canvas>
                <ContentPresenter Grid.Row="1" 
                                  Content="{TemplateBinding Content}"
                                  HorizontalAlignment="Center" />
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="lineOff" Property="Visibility"
                            Value="Hidden" />
                    <Setter TargetName="lineOn" Property="Visibility"
                            Value="Visible" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Page.Resources>

    <CheckBox Template="{StaticResource switch}"
              Content="Customized CheckBox"
              HorizontalAlignment="Center"
              VerticalAlignment="Center" />
</Page>
WPF Customized Check Box








24.17.CheckBox
24.17.1.Add CheckBox to StackPanelAdd CheckBox to StackPanel
24.17.2.Three-State CheckBoxThree-State CheckBox
24.17.3.A CheckBox with a skew transformationA CheckBox with a skew transformation
24.17.4.Customized CheckBoxCustomized CheckBox
24.17.5.CheckBox StyleCheckBox Style
24.17.6.CheckBox ListCheckBox List