使用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>

效果:

相关推荐
Scout-leaf10 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
柒.梧.12 天前
基于SpringBoot+JWT 实现Token登录认证与登录人信息查询
wpf
十月南城15 天前
Flink实时计算心智模型——流、窗口、水位线、状态与Checkpoint的协作
大数据·flink·wpf
听麟18 天前
HarmonyOS 6.0+ 跨端会议助手APP开发实战:多设备接续与智能纪要全流程落地
分布式·深度学习·华为·区块链·wpf·harmonyos
@hdd18 天前
Kubernetes 可观测性:Prometheus 监控、日志采集与告警
云原生·kubernetes·wpf·prometheus
zls36536518 天前
C# WPF canvas中绘制缺陷分布map
开发语言·c#·wpf
专注VB编程开发20年18 天前
c#Redis扣款锁的设计,多用户,多台电脑操作
wpf
闲人编程19 天前
定时任务与周期性调度
分布式·python·wpf·调度·cron·定时人物·周期性
zls36536519 天前
C# WPF canvas中绘制缺陷分布map并实现缩放
开发语言·c#·wpf
数据知道20 天前
PostgreSQL:Citus 分布式拓展,水平分片,支持海量数据与高并发
分布式·postgresql·wpf