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

打印 上一主题 下一主题

主题 890|帖子 890|积分 2670

  1. <Window x:Class="WPF练习.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:WPF练习"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow"
  9.         Height="450"
  10.         Width="800">
  11.     <Window.Resources>
  12.         <ControlTemplate TargetType="CheckBox"
  13.                          x:Key="SwitchButtonTemp">
  14.             <Border CornerRadius="5"
  15.                     BorderBrush="Gray"
  16.                     BorderThickness="1">
  17.                 <Grid>
  18.                     <TextBlock Text="开启"
  19.                                VerticalAlignment="Center" Margin="10,0,0,0"/>
  20.                     <TextBlock Text="关闭"
  21.                                VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,10,0"/>
  22.                     <Border Width="40"
  23.                             Height="26"
  24.                             Background="Orange"
  25.                             CornerRadius="5"
  26.                             HorizontalAlignment="Left"
  27.                             Margin="1,0,0,0"
  28.                             Name="border" />
  29.                 </Grid>
  30.                
  31.             </Border>
  32.             <ControlTemplate.Triggers>
  33.                 <Trigger Property="IsChecked"
  34.                          Value="True">
  35.                     <Setter Property="Margin"
  36.                             Value="47,0,0,0"
  37.                             TargetName="border">
  38.                     
  39.                     </Setter>
  40.                 </Trigger>
  41.             </ControlTemplate.Triggers>
  42.         </ControlTemplate>
  43.         <Style x:Key="FocusVisual">
  44.             <Setter Property="Control.Template">
  45.                 <Setter.Value>
  46.                     <ControlTemplate>
  47.                         <Rectangle Margin="2" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>
  48.                     </ControlTemplate>
  49.                 </Setter.Value>
  50.             </Setter>
  51.         </Style>
  52.       
  53.     </Window.Resources>
  54.     <Grid>
  55.         <StackPanel>
  56.             <CheckBox Template="{StaticResource SwitchButtonTemp}"
  57.                       Height="30"
  58.                       Width="90"
  59.                       IsChecked="True" />
  60.         </StackPanel>
  61.     </Grid>
  62. </Window>
复制代码
效果:



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

老婆出轨

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表