2018-8-10-WPF-修改按钮按下的颜色
title | author | date | CreateTime | categories |
---|---|---|---|---|
WPF 修改按钮按下的颜色
|
lindexi
|
2018-08-10 19:16:53 +0800
|
2018-03-15 20:26:22 +0800
|
WPF
|
本文告诉大家如何使用附加属性修改按钮按下去时的背景
先让大家看个图片,下面来告诉大家如何做
首先在后台创建一个附加属性
- public class ButtonBrush
- {
- public static readonly DependencyProperty ButtonPressBackgroundProperty = DependencyProperty.RegisterAttached(
- "ButtonPressBackground", typeof(Brush), typeof(ButtonBrush), new PropertyMetadata(default(Brush)));
- public static void SetButtonPressBackground(DependencyObject element, Brush value)
- {
- element.SetValue(ButtonPressBackgroundProperty, value);
- }
- public static Brush GetButtonPressBackground(DependencyObject element)
- {
- return (Brush) element.GetValue(ButtonPressBackgroundProperty);
- }
- }
然后在 xaml 使用附加属性
- <Button Margin="10,10,10,10"
- Width="300" Height="100"
- Content="确定"
- local:ButtonBrush.ButtonPressBackground="#FFfcac1c" />
如何在按钮按下时使用这个附加属性修改按钮颜色?实际重写按钮的样式可以看到,在按下时可以修改颜色
- <Style x:Key="Style.OkOperationButton"
- TargetType="ButtonBase">
- <Setter Property="Width" Value="110" />
- <Setter Property="Height" Value="44" />
- <Setter Property="FontSize" Value="24" />
- <Setter Property="Background" Value="#FF0087FF" />
- <Setter Property="HorizontalContentAlignment" Value="Center" />
- <Setter Property="VerticalContentAlignment" Value="Center" />
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ButtonBase}">
- <Border x:Name="Border" Width="{TemplateBinding Width}"
- Height="{TemplateBinding Height}"
- CornerRadius="22" Background="{TemplateBinding Background}">
- <TextBlock x:Name="TextBlock"
- Text="{TemplateBinding Content}"
- FontSize="{TemplateBinding FontSize}"
- HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
- VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsPressed" Value="True">
- <Setter Property="Background"
- Value="#FFfcac1c" />
- <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
- </Trigger>
- <Trigger Property="IsEnabled" Value="False">
- <Setter TargetName="Border" Property="Background" Value="#4D0087FF" />
- <Setter TargetName="TextBlock" Property="Foreground" Value="#4DFFFFFF" />
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
那么如何在设置使用附加属性,实际上使用下面的代码直接从按钮获取附加属性
- <Trigger Property="IsPressed" Value="True">
- <Setter Property="Background"
- Value="{Binding RelativeSource = {RelativeSource Self},Path=(local:ButtonBrush.ButtonPressBackground)}" />
- <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
- </Trigger>
所有的代码
- <Window.Resources>
- <Style x:Key="Style.OkOperationButton"
- TargetType="ButtonBase">
- <Setter Property="Width" Value="110" />
- <Setter Property="Height" Value="44" />
- <Setter Property="FontSize" Value="24" />
- <Setter Property="Background" Value="#FF0087FF" />
- <Setter Property="HorizontalContentAlignment" Value="Center" />
- <Setter Property="VerticalContentAlignment" Value="Center" />
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ButtonBase}">
- <Border x:Name="Border" Width="{TemplateBinding Width}"
- Height="{TemplateBinding Height}"
- CornerRadius="22" Background="{TemplateBinding Background}">
- <TextBlock x:Name="TextBlock"
- Text="{TemplateBinding Content}"
- FontSize="{TemplateBinding FontSize}"
- HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
- VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsPressed" Value="True">
- <Setter Property="Background"
- Value="{Binding RelativeSource = {RelativeSource Self},Path=(local:ButtonBrush.ButtonPressBackground)}" />
- <Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
- </Trigger>
- <Trigger Property="IsEnabled" Value="False">
- <Setter TargetName="Border" Property="Background" Value="#4D0087FF" />
- <Setter TargetName="TextBlock" Property="Foreground" Value="#4DFFFFFF" />
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
- <Grid>
- <Button Margin="10,10,10,10" Style="{StaticResource Style.OkOperationButton}"
- Width="300" Height="100"
- Content="确定"
- local:ButtonBrush.ButtonPressBackground="#FFfcac1c" />
- </Grid>
代码:下载
2018-8-10-WPF-修改按钮按下的颜色的更多相关文章
- UI设计篇·入门篇·绘制简单自定义矩形图/设置按钮按下弹起颜色变化/设置图形旋转
Android的基本控件和图形有限,难以满足所有的实际需要和设计需求,好在Android给出了相对完善的图形绘制和自定义控件的API,利用这些API,可以基本满足设计的需求. 自定义图像和控件的方法: ...
- WPF界面按钮美化
在App.xaml里加入全局按钮样式 <Application x:Class="WpfButton.App" xmlns="http://schemas.micr ...
- 申请Office 365一年免费的开发者账号攻略(2018年10月份版本)
要进行Office 365开发,当然需要有完整的Office 365环境才可以.为了便于广大开发人员快速地启动这项工作,微软官方给所有开发人员提供了免费的一年开发者账号 那么如何申请Office ...
- 01 mybatis框架整体概况(2018.7.10)-
01 mybatis框架整体概况(2018.7.10)- F:\廖雪峰 JavaEE 企业级分布式高级架构师课程\廖雪峰JavaEE一期\第一课(2018.7.10) maven用的是3.39的版本 ...
- 写自己的WPF样式 - 按钮
做一个后台管理小程序,据说WPF的界面比较"炫",于是选择使用WPF来开发.既然用了WPF当然需要做好看点了,于是稍微研究了下WPF的样式,废话不多说下面开始自定义一个按钮样式: ...
- IntelliJ IDEA 最新激活码(截止到2018年10月14日)
IntelliJ IDEA 注册码: EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUlXU1dEIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYX ...
- 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 ...
- [转载][转]修改/proc目录下的参数优化网络性能
原文地址:[转]修改/proc目录下的参数优化网络性能作者:雪人 网络优化 注意: 1. 参数值带有速度(rate)的参数不能在loopback接口上工作. 2.因为内核是以HZ为单位的内部时钟来定义 ...
- 国外10个ASP.Net C#下的开源CMS
国外10个ASP.Net C#下的开源CMS https://blog.csdn.net/peng_hai_lin/article/details/8612895 1.Ludico Ludico是 ...
随机推荐
- 使用线程 Monitor.Wait() 和 Monitor.Pulse()
Wait() 和 Pulse() 机制用于线程间交互.当在一个对象上使用Wait() 方法时,访问这个对象的线程就会一直等待直到被唤醒.Pulse() 和 PulseAll() 方法用来通知等待的 ...
- 做网站-Http状态码详解
https://mp.weixin.qq.com/s/ZcYG59yLsLCNY2-2k4YqwA HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码 ...
- 深度优先搜索(Depth-First-Search)精髓
引例:迷宫问题 首先我们来想象一只老鼠,在一座不见天日的迷宫内,老鼠在入口处进去,要从出口出来.那老鼠会怎么走?当然可以是这样的:老鼠如果遇到直路,就一直往前走,如果遇到分叉路口,就任意选择其中的一条 ...
- springmvc 使用了登录拦截器之后静态资源还是会被拦截的处理办法
解决办法 在拦截器的配置里加上静态资源的处理 参考https://www.jb51.net/article/103704.htm
- Linux ifconfig 配置网络接口
Linux ifconfig 可以用来配置网络接口的IP地址.掩码.网关.物理地址等:值得一说的是用Linux ifconfig 为网卡指定IP地址,这只是用来调试网络用的,并不会更改系统关于网卡的配 ...
- Leetcode77. Combinations组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3] ...
- Leetcode40. Combination Sum组合总数2 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- 【python之路16】lambda表达式
1.lambda表达式,实际是建立一个简易的函数 下面代码中f1和f2表示是相同过程的代码 def f1(args): return args f2 = lambda args:args print( ...
- HTML input type=file文件选择表单的汇总(一)
HTML input type=file 在onchange上传文件的过程中,遇到同一个文件二次上传无效的问题. 最近在做项目过程中,遇到同一文件上传的时候,二次上传没有效果,找了资料,找到了原因: ...
- truncate 、delete、drop的区别
TRUNCATE TABLE 在功能上与不带 Where 子句的 Delete 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 Delete 速度快,且使用的系统和事务日志资源 ...