找回密码
 立即注册

QQ登录

只需一步,快速开始

visualmaster
论坛元老   /  发表于:2015-9-15 17:07  /   查看:6270  /  回复:1
Switch 的控件其实有很多现成的,但是这次遇到了要把字体放里边,而后又要求改成圆角的,分享出来大家拿来直接用吧,基于CheckBox 改的样式
  1. <Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
  2.         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
  3.         <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
  4.         <Setter Property="Template">
  5.             <Setter.Value>
  6.                 <ControlTemplate TargetType="{x:Type CheckBox}">
  7.                     <ControlTemplate.Resources>
  8.                         <Storyboard x:Key="OnChecking" />
  9.                         <Storyboard x:Key="OnUnchecking" />
  10.                     </ControlTemplate.Resources>
  11.                     <DockPanel x:Name="dockPanel">
  12.                         <ContentPresenter x:Name="contentPresenter"
  13.                                           VerticalAlignment="Center"
  14.                                           Content="{TemplateBinding Content}"
  15.                                           ContentStringFormat="{TemplateBinding ContentStringFormat}"
  16.                                           ContentTemplate="{TemplateBinding ContentTemplate}"
  17.                                           RecognizesAccessKey="True"
  18.                                           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
  19.                         <Grid Width="70" Height="30">
  20.                             <Grid Margin="5">
  21.                                 <!--<Border BorderBrush="#FFA59F93" BorderThickness="2">-->
  22.                                 <Grid>
  23.                                     <TextBlock x:Name="OnTextBlock"
  24.                                                Margin="0,0,8,0"
  25.                                                HorizontalAlignment="Left"
  26.                                                VerticalAlignment="Center"
  27.                                                Panel.ZIndex="1"
  28.                                                Foreground="White"
  29.                                                Text="已开启"
  30.                                                Visibility="Collapsed" />
  31.                                     <TextBlock x:Name="OffTextBlock"
  32.                                                Margin="8,0,0,0"
  33.                                                HorizontalAlignment="Right"
  34.                                                VerticalAlignment="Center"
  35.                                                Panel.ZIndex="1"
  36.                                                Foreground="White"
  37.                                                Text="已关闭"
  38.                                                Visibility="Visible" />
  39.                                     <Path x:Name="OffRectangle"
  40.                                           Data="M9.99953079223633,0L10,1.2516975402832E-05 49.9995307922363,0 50,0 50.5132293701172,0.0130373239517212C55.7882423400879,0.281379029154778 60,4.65831650793552 60,10.0004428774118 60,15.3416608721018 55.7882423400879,19.7186187654734 50.5132293701172,19.9869636446238L50,19.9999889284372 49.9995307922363,20.000000372529 10,20.000000372529 9.99953079223633,20.000000372529C4.48505210876465,20.000000372529 -1.04166664589195E-07,15.5139593034983 1.81447628664201E-15,10.0004428774118 -1.04166664589195E-07,4.48598946630955 4.48505210876465,3.20445696644356E-07 9.99953079223633,0z"
  41.                                           Fill="#FFA59F93" />
  42.                                     <Path x:Name="OnRectangle"
  43.                                           Data="M9.99953079223633,0L10,1.2516975402832E-05 49.9995307922363,0 50,0 50.5132293701172,0.0130373239517212C55.7882423400879,0.281379029154778 60,4.65831650793552 60,10.0004428774118 60,15.3416608721018 55.7882423400879,19.7186187654734 50.5132293701172,19.9869636446238L50,19.9999889284372 49.9995307922363,20.000000372529 10,20.000000372529 9.99953079223633,20.000000372529C4.48505210876465,20.000000372529 -1.04166664589195E-07,15.5139593034983 1.81447628664201E-15,10.0004428774118 -1.04166664589195E-07,4.48598946630955 4.48505210876465,3.20445696644356E-07 9.99953079223633,0z"
  44.                                           Fill="#FF69C112"
  45.                                           Visibility="Collapsed" />
  46.                                 </Grid>
  47.                                 <!--</Border>-->
  48.                                 <Ellipse x:Name="slider"
  49.                                          Width="20"
  50.                                          Height="20"
  51.                                          HorizontalAlignment="Left"
  52.                                          Fill="#F6F7F6" />
  53.                             </Grid>
  54.                         </Grid>
  55.                     </DockPanel>
  56.                     <ControlTemplate.Triggers>
  57.                         <Trigger Property="IsChecked" Value="True">
  58.                             <Trigger.ExitActions>
  59.                                 <BeginStoryboard x:Name="OnUnchecking_BeginStoryboard" Storyboard="{StaticResource OnUnchecking}" />
  60.                             </Trigger.ExitActions>
  61.                             <Trigger.EnterActions>
  62.                                 <BeginStoryboard x:Name="OnChecking_BeginStoryboard" Storyboard="{StaticResource OnChecking}" />
  63.                             </Trigger.EnterActions>
  64.                             <Setter TargetName="OffTextBlock" Property="Visibility" Value="Collapsed" />
  65.                             <Setter TargetName="OnRectangle" Property="Visibility" Value="Visible" />
  66.                             <Setter TargetName="slider" Property="HorizontalAlignment" Value="Right" />
  67.                             <Setter TargetName="OnTextBlock" Property="Panel.ZIndex" Value="1" />
  68.                             <Setter TargetName="OnTextBlock" Property="Visibility" Value="Visible" />
  69.                         </Trigger>
  70.                         <Trigger Property="IsEnabled" Value="False">
  71.                             <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
  72.                         </Trigger>
  73.                     </ControlTemplate.Triggers>
  74.                 </ControlTemplate>
  75.             </Setter.Value>
  76.         </Setter>
  77.     </Style>
复制代码




  1. <Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
  2.         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
  3.         <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
  4.         <Setter Property="Template">
  5.             <Setter.Value>
  6.                 <ControlTemplate TargetType="{x:Type CheckBox}">
  7.                     <ControlTemplate.Resources>
  8.                         <Storyboard x:Key="OnChecking"/>
  9.                         <Storyboard x:Key="OnUnchecking"/>
  10.                     </ControlTemplate.Resources>
  11.                     <DockPanel x:Name="dockPanel">
  12.                         <ContentPresenter VerticalAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" x:Name="contentPresenter" ContentStringFormat="{TemplateBinding ContentStringFormat}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
  13.                         <Grid Height="35" Width="70">
  14.                             <Grid Margin="5">
  15.                                 <Border BorderBrush="#FFA59F93" BorderThickness="2">
  16.                                     <Grid Margin="2">
  17.                                         <TextBlock Panel.ZIndex="1" VerticalAlignment="Center" Visibility="Collapsed" Text="On" x:Name="OnTextBlock" Margin="0,0,3,0" HorizontalAlignment="Left"/>
  18.                                         <TextBlock Panel.ZIndex="1" VerticalAlignment="Center" Visibility="Visible" Text="Off" x:Name="OffTextBlock" Margin="3,0,0,0" HorizontalAlignment="Right"/>
  19.                                         <Rectangle Fill="#FFA59F93" x:Name="OffRectangle"/>
  20.                                         <Rectangle Fill="#CC119EDA" x:Name="OnRectangle" Visibility="Collapsed"/>
  21.                                     </Grid>
  22.                                 </Border>
  23.                                 <Rectangle Width="13" x:Name="slider" HorizontalAlignment="Left" Fill="Black"/>
  24.                             </Grid>
  25.                         </Grid>
  26.                     </DockPanel>
  27.                     <ControlTemplate.Triggers>
  28.                         <Trigger Value="True" Property="IsChecked">
  29.                             <Trigger.ExitActions>
  30.                                 <BeginStoryboard x:Name="OnUnchecking_BeginStoryboard" Storyboard="{StaticResource OnUnchecking}"/>
  31.                             </Trigger.ExitActions>
  32.                             <Trigger.EnterActions>
  33.                                 <BeginStoryboard x:Name="OnChecking_BeginStoryboard" Storyboard="{StaticResource OnChecking}"/>
  34.                             </Trigger.EnterActions>
  35.                             <Setter Property="Visibility" TargetName="OffTextBlock" Value="Collapsed"/>
  36.                             <Setter Property="Visibility" TargetName="OnRectangle" Value="Visible"/>
  37.                             <Setter Property="HorizontalAlignment" TargetName="slider" Value="Right"/>
  38.                             <Setter Property="Panel.ZIndex" TargetName="OnTextBlock" Value="1"/>
  39.                             <Setter Property="Visibility" TargetName="OnTextBlock" Value="Visible"/>
  40.                         </Trigger>
  41.                         <Trigger Property="IsEnabled" Value="False">
  42.                             <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
  43.                         </Trigger>
  44.                     </ControlTemplate.Triggers>
  45.                 </ControlTemplate>
  46.             </Setter.Value>
  47.         </Setter>
  48.     </Style>
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

评分

参与人数 1金币 +999 收起 理由
Alice + 999 谢谢详细的代码。 给以后使用的用户提供此方面的思路。 奖励金币。

查看全部评分

1 个回复

倒序浏览
Alice
社区贡献组   /  发表于:2015-9-15 17:48:00
沙发
回复 1楼visualmaster的帖子

谢谢反馈。
给以后使用的用户提供相关思路。
奖励金币。
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部