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

效果:

相关推荐
攻城狮CSU4 小时前
WPF中核心接口 INotifyPropertyChanged
wpf
c#上位机4 小时前
wpf之Interaction.Triggers
c#·wpf
是木子啦8 小时前
wpf passwordbox控件 光标移到最后
c#·wpf
The Sheep 20238 小时前
wpf 命令理解
wpf
布伦鸽8 小时前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf
分布式存储与RustFS20 小时前
告别复杂配置:用Milvus、RustFS和Vibe Coding,60分钟DIY专属Chatbot
wpf·文件系统·milvus·对象存储·minio·rustfs·vibe
攻城狮CSU1 天前
WPF 绑定机制实现原理
wpf
攻城狮CSU1 天前
WPF 之数据绑定一(Data Binding)
wpf
wuty0072 天前
记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题
wpf·进程间通信·sendmessage·进程权限
c#上位机2 天前
wpf之ToggleButton控件
c#·wpf