使用WPF写一个简单的开关控件

XML 复制代码
<Window x:Class="WPF练习.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF练习"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">
    <Window.Resources>
        <ControlTemplate TargetType="CheckBox"
                         x:Key="SwitchButtonTemp">
            <Border CornerRadius="5"
                    BorderBrush="Gray"
                    BorderThickness="1">
                <Grid>
                    <TextBlock Text="开启"
                               VerticalAlignment="Center" Margin="10,0,0,0"/>
                    <TextBlock Text="关闭"
                               VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,10,0"/>
                    <Border Width="40"
                            Height="26"
                            Background="Orange"
                            CornerRadius="5"
                            HorizontalAlignment="Left"
                            Margin="1,0,0,0"
                            Name="border" />
                </Grid>
                
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked"
                         Value="True">
                    <Setter Property="Margin"
                            Value="47,0,0,0"
                            TargetName="border">
                    
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <Style x:Key="FocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="2" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
       
    </Window.Resources>
    <Grid>
        <StackPanel>
            <CheckBox Template="{StaticResource SwitchButtonTemp}"
                      Height="30"
                      Width="90"
                      IsChecked="True" />
        </StackPanel>
    </Grid>
</Window>

效果:

相关推荐
aGdF8E3gQ16 小时前
【Application Insights】采样率对Function App日志收集的影响和解决方法
python·flask·wpf
程序员-Benothing1 天前
如何实现高并发系统订单30分钟自动关闭?
开发语言·c#·wpf
一水2 天前
Redis实战:一个AI工作流系统里的五个应用场景,从传参到限流的完整链路
数据库·redis·wpf
dalong102 天前
WPF:3D甜甜圈
3d·c#·wpf·甜甜圈
别催小唐敲代码2 天前
ROS2从入门到实战:去中心化机器人操作系统详解
wpf·ros2
脚踏实地皮皮晨3 天前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨3 天前
003005002_WPF StackPanel 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
脚踏实地皮皮晨3 天前
003004002_UniformGrid控件的使用方法
开发语言·c#·.net·wpf·visual studio
脚踏实地皮皮晨4 天前
003003002_WPF Grid 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
脚踏实地皮皮晨5 天前
003002004_WPF Panel 基类 官方类定义
开发语言·windows·算法·c#·wpf·visual studio