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

效果:

相关推荐
dotent·15 小时前
一个 WPF 文档和工具窗口布局容器
wpf
c#上位机15 小时前
wpf之ComboBox
wpf
lindexi19 小时前
WPF 引用 ASP.NET Core 的 AOT 版本
wpf·asp.netcore
我好喜欢你~1 天前
WPF---数据模版
wpf
hqwest2 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest3 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple3 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题4 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘4 天前
开发WPF项目时遇到的问题总结
wpf
hqwest5 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel