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

效果:

相关推荐
我要打打代码26 分钟前
WPF入门指南(1)
wpf
一叶星殇1 小时前
WPF UI 框架大全(2026版)
ui·wpf
我要打打代码3 小时前
WPF入门指南(3)
wpf
ALex_zry3 小时前
Redis Cluster 故障转移与高可用实践
数据库·redis·wpf
kylezhao20193 小时前
深入浅出理解 C# WPF 的事件
开发语言·c#·wpf
FuckPatience1 天前
WPF xaml中的 xmlns:d=“http://schemas.microsoft.com/expression/blend/2008“ 的使用
ui·wpf
就是有点傻1 天前
WPF加载动画
ui·wpf
kylezhao20191 天前
WPF Binding 从入门到精通
wpf
Poetinthedusk2 天前
WPF应用跟随桌面切换
开发语言·wpf
小北方城市网2 天前
MongoDB 分布式存储与查询优化:从副本集到分片集群
java·spring boot·redis·分布式·wpf