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是 ...
随机推荐
- myeclipse10.7的破解 不需要去CSDN付费下载-免csdn费下载
吐槽一下,大票CSDN博主,在博文里基本不放干货,都弄成附件,放在csdn付费下载,一个破解办法,竟然50元,好在我是vip用户,不在乎价格,特此 这篇文章搬运一下资源给大家免费下载 顺便纠正一下其文 ...
- Hibernate4的注解 (持续更新范例中)
作用:使得Hibernate程序的开发大大的简化.利用注解后,可不用定义持久化类对应的*.hbm.xml,而直接以注解方式写入持久化类的实现中. 注解配置持久化类常用注解. 注解 含义和作用 @Ent ...
- jQuery 图片上传
1. 概述 1.1 说明 在一些前后端不分离的项目中,经常有一些需要把文件或者图片上传的功能,故记录此代码以便后期使用. 1.2 要求 1.上传,10M以内,限bmp,jpg,png,jpeg等图片格 ...
- JS的八大数据类型
js中的数据类型,包括基本数据类型(Number,String,Boolean, Undefined,Null)和 复杂(引用)数据类型(Object,Array,Function) 基本数据类型 ...
- Mac上代码开启dump的core文件生成方案
#ifdef Q_OS_MAC struct rlimit rl; getrlimit(RLIMIT_NOFILE,&rl); rl.rlim_cur = qMin((rlim_t)OPEN_ ...
- HZOI20190714 T3建造游乐场
先放作者的正解: 先说g吧,有i个点的话,在其中i-1个点中有$C_{i-1}^{2}$种边,每个边有选和不选两种情况.如果度不是偶数呢?用剩下那个点给他连上呗.如果剩下那个点度数不是偶数呢?这是不可 ...
- git安装方法
点击 http://git-scm.com/download 选择你使用的操作系统,如果是linux就可以看到命令行,windows则是下载客户端
- 【JZOJ4787】【NOIP2016提高A组模拟9.17】数格子
题目描述 输入 输出 样例输入 1 10000 3 10000 5 10000 0 0 样例输出 1 11 95 数据范围 每个测试点数据组数不超过10组 解法 状态压缩动态规划. 设f[i][j]表 ...
- 【7.19 graphshortestpath graphallshortestpaths函数】matlab 求最短路径函数总结
graphshortestpath 函数是用来解决最短路径问题的. 语法为: [dist, path, pred]=graphshortestpath(G,S) [dist, path, pred]= ...
- Directx11教程(67) 显示模型文件
原文:Directx11教程(67) 显示模型文件 在前面的教程中,我们都是通过在ModelClass中直接产生顶点和索引数据,简单的三角形,立方体等等还好说,毕竟比较简单,如何显示复杂的 ...