今天在开发的过程中由于会用到wpf的ToggleButton来控制开关,于是在度娘寻觅终得一位佳人分享。

<LinearGradientBrush x:Key="ButtonNormalBackgroundFill" EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FFFFFFFF" Offset="0"/><GradientStop Color="#FFF0F0EA" Offset="0.9"/></LinearGradientBrush><SolidColorBrush x:Key="ButtonBorder" Color="#FF003C74"/><Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}"><Setter Property="FocusVisualStyle" Value="{x:Null}"/><Setter Property="Background" Value="{StaticResource ButtonNormalBackgroundFill}"/><Setter Property="BorderBrush" Value="{StaticResource ButtonBorder}"/><Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/><Setter Property="HorizontalContentAlignment" Value="Center"/><Setter Property="VerticalContentAlignment" Value="Center"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ToggleButton}"><ControlTemplate.Resources><Storyboard x:Key="OnChecked1"><ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path"><EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="40,0,0,0"/></ThicknessAnimationUsingKeyFrames><ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path1"><EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="40,0,0,0"/></ThicknessAnimationUsingKeyFrames><ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path2"><EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="40,0,0,0"/></ThicknessAnimationUsingKeyFrames><StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="displayText"><DiscreteStringKeyFrame KeyTime="0" Value="OFF"/></StringAnimationUsingKeyFrames></Storyboard><Storyboard x:Key="OnUnchecked1"><ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path"><EasingThicknessKeyFrame KeyTime="0" Value="40,0,0,0"/><EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="0"/></ThicknessAnimationUsingKeyFrames><ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path1"><EasingThicknessKeyFrame KeyTime="0" Value="40,0,0,0"/><EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="0"/></ThicknessAnimationUsingKeyFrames><ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="path2"><EasingThicknessKeyFrame KeyTime="0" Value="40,0,0,0"/><EasingThicknessKeyFrame KeyTime="0:0:0.5" Value="0"/></ThicknessAnimationUsingKeyFrames><StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="displayText"><DiscreteStringKeyFrame KeyTime="0" Value="ON"/></StringAnimationUsingKeyFrames><ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="displayText"><EasingColorKeyFrame KeyTime="0" Value="White"/><EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF63FA00"/></ColorAnimationUsingKeyFrames></Storyboard></ControlTemplate.Resources><Border CornerRadius="10" Background="#FF3B3939" Width="60" Height="20"><Grid><TextBlock x:Name="displayText" Text="ON" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock><Path x:Name="path2" Fill="Black"><Path.Data><GeometryGroup><GeometryGroup.Children><EllipseGeometry Center="10,10" RadiusX="8.5" RadiusY="8.5"></EllipseGeometry></GeometryGroup.Children></GeometryGroup></Path.Data></Path><Path x:Name="path1" Fill="#FF818080"><Path.Data><GeometryGroup><GeometryGroup.Children><EllipseGeometry Center="10,10" RadiusX="7" RadiusY="7"></EllipseGeometry></GeometryGroup.Children></GeometryGroup></Path.Data></Path><Path x:Name="path"><Path.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF807F7F" Offset="0"/><GradientStop Color="#FF373636" Offset="1"/></LinearGradientBrush></Path.Fill><Path.Data><GeometryGroup><GeometryGroup.Children><EllipseGeometry Center="10,10" RadiusX="8" RadiusY="8"></EllipseGeometry></GeometryGroup.Children></GeometryGroup></Path.Data></Path></Grid></Border><ControlTemplate.Triggers><EventTrigger RoutedEvent="ToggleButton.Checked"><BeginStoryboard Storyboard="{StaticResource OnChecked1}"/></EventTrigger><EventTrigger RoutedEvent="ToggleButton.Unchecked"><BeginStoryboard x:Name="OnUnchecked1_BeginStoryboard" Storyboard="{StaticResource OnUnchecked1}"/></EventTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>

我把它链接分享给大家吧。原文链接:https://blog.csdn.net/anjingyatou/article/details/17417207

wpf ToggleButton自定义样式相关推荐

  1. WPF初学——自定义样式

    在WPF开发界面的过程中,经常会遇到多个相同种类的控件样式也是统一的,但是要一个一个地去设置,即使是复制粘贴都嫌累,所以翻了些书,网上度娘了一些材料,发现WPF有很容易的方式解决我的纠结,那就是--自 ...

  2. C# wpf ScrollBar自定义样式详解

    文章目录 前言 一.ScrollBar的组成 1.结构说明 2.具体组成 行按钮 页按钮 滑块 轨道 二.如何自定义? 1.确定参数 2.定义Style 3.定义Template 四.使用方法 1.继 ...

  3. WPF DataGrid自定义样式模板 列表头分隔线 滚动条滑块大小设定 动态数据绑定和更新

    [ 效果图回去放,代码在后面 ] [ 用到的一些定义如果没有附代码可以随便写个看下效果,因为直接从项目中copy出来的,难免漏掉点点... ] 首先,有几点需要注意: 1.表头样式 [ DataGri ...

  4. wpf -----Expander自定义样式

    Expander是一个可以展开和折叠的控件,由ContentPresenter和ToggleButton组成 <!--Expander样式--> <Style x:Key=" ...

  5. WPF CheckBox自定义样式

    今天整理了两个CheckBox样式,用鼠标选择时都是滑动效果,觉得还不错,特记录一下,效果图如下: 以下是代码,直接粘贴的创建的Windows中,运行就可以了. <Window.Resource ...

  6. WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...

  7. WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

    原文:WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展 一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐 ...

  8. WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式

    原文:WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式 一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等, ...

  9. WPF自定义控件与样式(13)-自定义窗体Window 自适应内容大小消息框MessageBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 自定义 ...

最新文章

  1. sql server 的 money类型
  2. jar包和war包区别及理解
  3. iphoneX样式兼容
  4. 叠置分析(涉及结果属性和输入图层属性的关系)
  5. oracle parameterfile
  6. 打造 AI 语音新标杆,英特尔与腾讯云小微创新共赢
  7. con排插与单片机相连_教你如何使用Labview和单片机通信(串口).pdf
  8. ADO SQL手写分页
  9. iis6 元数据库与iis6 配置的兼容 出错问题
  10. app逆向入门分析——破解某APP登陆请求参数
  11. 熟悉mysql数据库设计和性能优化_MySQL性能优化学习笔记-(1)数据库设计
  12. avoid mutating a prop directly since the value will be overwritten whenever完美解决
  13. 讯飞实时语音转写 python3.6.1 可完美运行 解析返回的json字符串 输出所获语音文字
  14. fatal: could not create work tree dir ‘xxx’: Permission denied解决办法
  15. 二年级计算机课,小学二年级信息技术课程教案三篇
  16. Leetcode——153. Find Minimum in Rotated Sorted Array
  17. 提问的智慧( 笔记)
  18. 【chatgpt插件-ChatGPT Sidebar】
  19. Android摄像头调用失败问题
  20. Win 10 环境下 eclipse 安装使用教程

热门文章

  1. 利用canvas给图片添加水印
  2. 用一行 Python 代码搞定炫酷可视化
  3. 服务启动错误1053,一例解决方案(给用户添加NetworkService权限)
  4. rank函数怎么用oracle,oracle rank 函数的使用
  5. markdown庆祝火影动漫即将完结
  6. Android Menu item位置,Android MenuItem自定义布局
  7. 知道个税怎么计算工资
  8. java 双向链表的示例
  9. 新玺配资:三大指数联袂上攻 量能变化非常关键
  10. Java 程序员 开发面试题