title author date CreateTime categories
WPF 修改按钮按下的颜色
lindexi
2018-08-10 19:16:53 +0800
2018-03-15 20:26:22 +0800
WPF

本文告诉大家如何使用附加属性修改按钮按下去时的背景

先让大家看个图片,下面来告诉大家如何做

首先在后台创建一个附加属性

  1. public class ButtonBrush
  2. {
  3. public static readonly DependencyProperty ButtonPressBackgroundProperty = DependencyProperty.RegisterAttached(
  4. "ButtonPressBackground", typeof(Brush), typeof(ButtonBrush), new PropertyMetadata(default(Brush)));
  5.  
  6. public static void SetButtonPressBackground(DependencyObject element, Brush value)
  7. {
  8. element.SetValue(ButtonPressBackgroundProperty, value);
  9. }
  10.  
  11. public static Brush GetButtonPressBackground(DependencyObject element)
  12. {
  13. return (Brush) element.GetValue(ButtonPressBackgroundProperty);
  14. }
  15. }

然后在 xaml 使用附加属性

  1. <Button Margin="10,10,10,10"
  2. Width="300" Height="100"
  3. Content="确定"
  4. local:ButtonBrush.ButtonPressBackground="#FFfcac1c" />

如何在按钮按下时使用这个附加属性修改按钮颜色?实际重写按钮的样式可以看到,在按下时可以修改颜色

  1. <Style x:Key="Style.OkOperationButton"
  2. TargetType="ButtonBase">
  3. <Setter Property="Width" Value="110" />
  4. <Setter Property="Height" Value="44" />
  5. <Setter Property="FontSize" Value="24" />
  6. <Setter Property="Background" Value="#FF0087FF" />
  7. <Setter Property="HorizontalContentAlignment" Value="Center" />
  8. <Setter Property="VerticalContentAlignment" Value="Center" />
  9. <Setter Property="Template">
  10. <Setter.Value>
  11. <ControlTemplate TargetType="{x:Type ButtonBase}">
  12. <Border x:Name="Border" Width="{TemplateBinding Width}"
  13. Height="{TemplateBinding Height}"
  14. CornerRadius="22" Background="{TemplateBinding Background}">
  15. <TextBlock x:Name="TextBlock"
  16. Text="{TemplateBinding Content}"
  17. FontSize="{TemplateBinding FontSize}"
  18. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
  19. VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
  20. </Border>
  21.  
  22. <ControlTemplate.Triggers>
  23. <Trigger Property="IsPressed" Value="True">
  24. <Setter Property="Background"
  25. Value="#FFfcac1c" />
  26. <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
  27. </Trigger>
  28. <Trigger Property="IsEnabled" Value="False">
  29. <Setter TargetName="Border" Property="Background" Value="#4D0087FF" />
  30. <Setter TargetName="TextBlock" Property="Foreground" Value="#4DFFFFFF" />
  31. </Trigger>
  32. </ControlTemplate.Triggers>
  33. </ControlTemplate>
  34. </Setter.Value>
  35. </Setter>
  36. </Style>

那么如何在设置使用附加属性,实际上使用下面的代码直接从按钮获取附加属性

  1. <Trigger Property="IsPressed" Value="True">
  2. <Setter Property="Background"
  3. Value="{Binding RelativeSource = {RelativeSource Self},Path=(local:ButtonBrush.ButtonPressBackground)}" />
  4. <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
  5. </Trigger>

所有的代码

  1. <Window.Resources>
  2.  
  3. <Style x:Key="Style.OkOperationButton"
  4. TargetType="ButtonBase">
  5. <Setter Property="Width" Value="110" />
  6. <Setter Property="Height" Value="44" />
  7. <Setter Property="FontSize" Value="24" />
  8. <Setter Property="Background" Value="#FF0087FF" />
  9. <Setter Property="HorizontalContentAlignment" Value="Center" />
  10. <Setter Property="VerticalContentAlignment" Value="Center" />
  11. <Setter Property="Template">
  12. <Setter.Value>
  13. <ControlTemplate TargetType="{x:Type ButtonBase}">
  14. <Border x:Name="Border" Width="{TemplateBinding Width}"
  15. Height="{TemplateBinding Height}"
  16. CornerRadius="22" Background="{TemplateBinding Background}">
  17. <TextBlock x:Name="TextBlock"
  18. Text="{TemplateBinding Content}"
  19. FontSize="{TemplateBinding FontSize}"
  20. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
  21. VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
  22. </Border>
  23.  
  24. <ControlTemplate.Triggers>
  25. <Trigger Property="IsPressed" Value="True">
  26. <Setter Property="Background"
  27. Value="{Binding RelativeSource = {RelativeSource Self},Path=(local:ButtonBrush.ButtonPressBackground)}" />
  28. <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
  29. </Trigger>
  30. <Trigger Property="IsEnabled" Value="False">
  31. <Setter TargetName="Border" Property="Background" Value="#4D0087FF" />
  32. <Setter TargetName="TextBlock" Property="Foreground" Value="#4DFFFFFF" />
  33. </Trigger>
  34. </ControlTemplate.Triggers>
  35. </ControlTemplate>
  36. </Setter.Value>
  37. </Setter>
  38. </Style>
  39.  
  40. </Window.Resources>
  41. <Grid>
  42. <Button Margin="10,10,10,10" Style="{StaticResource Style.OkOperationButton}"
  43. Width="300" Height="100"
  44. Content="确定"
  45. local:ButtonBrush.ButtonPressBackground="#FFfcac1c" />
  46. </Grid>

代码:下载

2018-8-10-WPF-修改按钮按下的颜色的更多相关文章

  1. UI设计篇·入门篇·绘制简单自定义矩形图/设置按钮按下弹起颜色变化/设置图形旋转

    Android的基本控件和图形有限,难以满足所有的实际需要和设计需求,好在Android给出了相对完善的图形绘制和自定义控件的API,利用这些API,可以基本满足设计的需求. 自定义图像和控件的方法: ...

  2. WPF界面按钮美化

    在App.xaml里加入全局按钮样式 <Application x:Class="WpfButton.App" xmlns="http://schemas.micr ...

  3. 申请Office 365一年免费的开发者账号攻略(2018年10月份版本)

    要进行Office 365开发,当然需要有完整的Office 365环境才可以.为了便于广大开发人员快速地启动这项工作,微软官方给所有开发人员提供了免费的一年开发者账号   那么如何申请Office ...

  4. 01 mybatis框架整体概况(2018.7.10)-

    01 mybatis框架整体概况(2018.7.10)- F:\廖雪峰 JavaEE 企业级分布式高级架构师课程\廖雪峰JavaEE一期\第一课(2018.7.10) maven用的是3.39的版本 ...

  5. 写自己的WPF样式 - 按钮

    做一个后台管理小程序,据说WPF的界面比较"炫",于是选择使用WPF来开发.既然用了WPF当然需要做好看点了,于是稍微研究了下WPF的样式,废话不多说下面开始自定义一个按钮样式: ...

  6. IntelliJ IDEA 最新激活码(截止到2018年10月14日)

    IntelliJ IDEA 注册码: EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUlXU1dEIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYX ...

  7. CAS (10) —— JBoss EAP 6.4下部署CAS时出现错误exception.message=Error decoding flow execution的解决办法

    CAS (10) -- JBoss EAP 6.4下部署CAS时出现错误exception.message=Error decoding flow execution的解决办法 jboss版本: jb ...

  8. [转载][转]修改/proc目录下的参数优化网络性能

    原文地址:[转]修改/proc目录下的参数优化网络性能作者:雪人 网络优化 注意: 1. 参数值带有速度(rate)的参数不能在loopback接口上工作. 2.因为内核是以HZ为单位的内部时钟来定义 ...

  9. 国外10个ASP.Net C#下的开源CMS

    国外10个ASP.Net C#下的开源CMS https://blog.csdn.net/peng_hai_lin/article/details/8612895   1.Ludico Ludico是 ...

随机推荐

  1. 使用线程 Monitor.Wait() 和 Monitor.Pulse()

      Wait() 和 Pulse() 机制用于线程间交互.当在一个对象上使用Wait() 方法时,访问这个对象的线程就会一直等待直到被唤醒.Pulse() 和 PulseAll() 方法用来通知等待的 ...

  2. 做网站-Http状态码详解

    https://mp.weixin.qq.com/s/ZcYG59yLsLCNY2-2k4YqwA HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码 ...

  3. 深度优先搜索(Depth-First-Search)精髓

    引例:迷宫问题 首先我们来想象一只老鼠,在一座不见天日的迷宫内,老鼠在入口处进去,要从出口出来.那老鼠会怎么走?当然可以是这样的:老鼠如果遇到直路,就一直往前走,如果遇到分叉路口,就任意选择其中的一条 ...

  4. springmvc 使用了登录拦截器之后静态资源还是会被拦截的处理办法

    解决办法 在拦截器的配置里加上静态资源的处理 参考https://www.jb51.net/article/103704.htm

  5. Linux ifconfig 配置网络接口

    Linux ifconfig 可以用来配置网络接口的IP地址.掩码.网关.物理地址等:值得一说的是用Linux ifconfig 为网卡指定IP地址,这只是用来调试网络用的,并不会更改系统关于网卡的配 ...

  6. Leetcode77. Combinations组合

    给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...

  7. Leetcode40. Combination Sum组合总数2 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  8. 【python之路16】lambda表达式

    1.lambda表达式,实际是建立一个简易的函数 下面代码中f1和f2表示是相同过程的代码 def f1(args): return args f2 = lambda args:args print( ...

  9. HTML input type=file文件选择表单的汇总(一)

    HTML input type=file 在onchange上传文件的过程中,遇到同一个文件二次上传无效的问题. 最近在做项目过程中,遇到同一文件上传的时候,二次上传没有效果,找了资料,找到了原因: ...

  10. truncate 、delete、drop的区别

    TRUNCATE TABLE 在功能上与不带 Where 子句的 Delete 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 Delete 速度快,且使用的系统和事务日志资源 ...