当前位置: 首页 > article >正文

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

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

效果:


http://www.kler.cn/news/365389.html

相关文章:

  • Rust的move关键字在线程中的使用
  • springboot3.x.x 集成 连接SQL Server 2008 驱动版本和SSL套接字问题的解决
  • Openpyxl--学习记录
  • Leetcode 140 Word Break II
  • arrylist怎么让他变得不可修改
  • 基于GA遗传优化的CNN-GRU-SAM网络时间序列回归预测算法matlab仿真
  • 小程序无法获取头像昵称以及手机号码的深度剖析与解决方案
  • 【Fargo】15: 使用mediasoup的UdpSocket发送数据
  • ubuntu20.04上使用 Verdaccio 搭建 npm 私有仓库
  • http 请求返回307
  • UI管理器的使用
  • SVN(Subversion)的介绍和使用
  • 在 Excel 中的单元格内开始一行新文本
  • idea项目搭建的四种方式: 一(以idea2017为例)
  • WPF中的ContentPresenter、ItemsPresenter、ScrollContentPresenter
  • P10424 [蓝桥杯 2024 省 B] 好数 题解
  • 机器学习与神经网络:发展历程及其对社会经济的深远影响
  • 内置数据类型、变量名、字符串、数字及其运算、数字的处理、类型转换
  • windows中git无法通过ssh连接github
  • Unreal Engine5安装Niagara UI Renderer插件
  • 鸿蒙实现相机拍照及相册选择照片
  • Spring Cloud --- Sentinel 熔断规则
  • 51单片机快速入门之 AD(模数) DA(数模) 转换 2024/10/25
  • react18中的函数组件底层渲染原理分析
  • Git的初次使用
  • 若依框架篇-若依集成 X-File-Storage 框架(实现图片上传阿里云 OSS 服务器)、EasyExcel 框架(实现 Excel 数据批量导入功能)