zoukankan      html  css  js  c++  java
  • WPF可切换按钮,iOS风格

    定义一个按钮样式

    <Window x:Class="WpfApp2可切换按钮.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:WpfApp2可切换按钮"
            mc:Ignorable="d"
            Title="MainWindow"
            Height="450"
            Width="800">
        <Window.Resources>
            <local:ToggleBtnCornorRadiusCvt x:Key="BtnCornorRadiusCvt"></local:ToggleBtnCornorRadiusCvt>
    
    
            <Style x:Key="ToggleButtonStyle1"
                   TargetType="{x:Type ToggleButton}">
                <Setter Property="FocusVisualStyle"
                        Value="{x:Null}" />
                <Setter Property="Background"
                        Value="White" />
                <Setter Property="BorderBrush"
                        Value="Gray" /> 
                <Setter Property="BorderThickness"
                        Value="1" />
                <Setter Property="HorizontalContentAlignment"
                        Value="Center" />
                <Setter Property="VerticalContentAlignment"
                        Value="Center" />
                <Setter Property="Padding"
                        Value="1" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}"> 
                            <Grid>
                                <Border Name="bd"
                                        Background="White"
                                        BorderBrush="Gray"
                                        BorderThickness="1" 
                                        CornerRadius="{Binding ActualHeight,RelativeSource={RelativeSource Self},Converter={StaticResource  BtnCornorRadiusCvt}}"></Border>
                                <Ellipse Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
                                         Stroke="Gray"
                                         Fill="White"
                                         HorizontalAlignment="Left"
                                         Name="ec"></Ellipse>
                            </Grid>
    
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked"
                                         Value="True">
                                    <Setter Property="HorizontalAlignment"
                                            Value="Right"
                                            TargetName="ec"></Setter>
                                    <Setter Property="Background"
                                            Value="Green"
                                            TargetName="bd"></Setter>
                                </Trigger>
    
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
        <Grid>
            <ToggleButton Style="{DynamicResource ToggleButtonStyle1}"></ToggleButton>
        </Grid>
    </Window>
    

      

    定义一个转换器

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Data;
    
    namespace WpfApp2可切换按钮
    {
       public class ToggleBtnCornorRadiusCvt:IValueConverter
    
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if(value is double  d)
                {
                    return d / 2;
                }
    
                return 0;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

      效果图

  • 相关阅读:
    java任务调度之Timer定时器
    springboot 启动的时候报java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
    Spring 体系结构
    为什么MySQL数据库要用B+树存储索引?
    Nginx反向代理服务器的安装与配置
    详细的最新版fastdfs单机版搭建
    FastDFS 分布式文件系统(部署和运维)
    linux
    Spring Cloud底层原理
    Spring中ioc的实现原理
  • 原文地址:https://www.cnblogs.com/congqiandehoulai/p/15254257.html
Copyright © 2011-2022 走看看