参考其他人demo,做了小修改。
<Style TargetType="CheckBox" > <Setter Property="Template" > <Setter.Value> <ControlTemplate TargetType="CheckBox"> <DockPanel> <ContentPresenter DockPanel.Dock="Left" VerticalAlignment="Center" Margin="10 0"></ContentPresenter> <Grid > <Grid.ColumnDefinitions> <ColumnDefinition Width="40"></ColumnDefinition> <ColumnDefinition Width="40"></ColumnDefinition> </Grid.ColumnDefinitions> <Rectangle Grid.Column="0" Grid.ColumnSpan="2" Fill="Gray"></Rectangle> <Border x:Name="gborder"> <TextBlock x:Name="txtInfo" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock> </Border> </Grid> </DockPanel> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="true"> <Setter TargetName="gborder" Property="Grid.Column" Value="1"></Setter> <Setter TargetName="gborder" Property="Background" Value="LightBlue"></Setter> <Setter TargetName="txtInfo" Property="Text" Value="On"></Setter> </Trigger> <Trigger Property="IsChecked" Value="false"> <Setter TargetName="gborder" Property="Grid.Column" Value="0"></Setter> <Setter TargetName="txtInfo" Property="Text" Value="Off"></Setter> </Trigger> <Trigger Property="IsChecked" Value="{x:Null}"> <Setter TargetName="gborder" Property="Grid.Column" Value="0"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
<CheckBox Content="开关" Width="200" Height="40"></CheckBox>
效果如下: