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-修改按钮按下的颜色的更多相关文章

  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. 【solr】SolrCloud中索引数据存储于HDFS

    SolrCloud中索引数据存储于HDFS 本人最近使用SolrCloud存储索引日志条件,便于快速索引,因为我的索引条件较多,每天日志记录较大,索引想到将日志存入到HDFS中,下面就说说怎么讲sol ...

  2. opencv java swing 图片灰度化 二值化

    工程下载地址 https://download.csdn.net/download/qq_16596909/11503860 基于maven 首先引入opencv <!-- https://mv ...

  3. 使用junit单元测试,报Cannot instantiate test(s): java.lang.SecurityException: Prohibited package name: java.com.com.test

    在测试类中不能一级包名不能以java开头, 将包改为com.com.test就好了.

  4. python 索引上的合并

  5. golang学习资料必备

    核心资料库 https://github.com/yangwenmai/learning-golang

  6. 悠星网络基于阿里云分析型数据库PostgreSQL版的数据实践

    说到“大数据”,当下这个词很火,各行各业涉及到数据的,目前都在提大数据,提数据仓库,数据挖掘或者机器学习,但同时另外一个热门的名词也很火,那就是“云”.越来越多的企业都在搭建属于自己的云平台,也有一些 ...

  7. bzoj 3895 取石子——博弈论

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3895 看题解:https://blog.csdn.net/popoqqq/article/d ...

  8. ASP.NET自定义控件组件开发 第一章 第三篇 第一章的完结篇

    ASP.NET自定义控件组件开发 第一章 第三篇   第三篇:第一章的完结篇 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ...

  9. HTML小技巧:按钮中的文字换行 .

    一般按钮的文字都是一行的.但是有的时候画面需要按钮中的文字换行. 刚开始有个开发人员说没法实现.\r\n 都用过了没有效果.其实google这个老师是非常强大的. 直接换行的方法:<input ...

  10. SDUT-3377_数据结构实验之查找五:平方之哈希表

    数据结构实验之查找五:平方之哈希表 Time Limit: 400 ms Memory Limit: 65536 KiB Problem Description 给定的一组无重复数据的正整数,根据给定 ...