需求:封装按钮,按钮上面只显示文字。在鼠标移上去、鼠标点击按钮、以及将按钮设为不可用时按钮的背景色和前景色需要发生变化

实现:继承Button类,封装如下6个属性:

#region 依赖属性
/// <summary>
/// 当鼠标移到按钮上时,按钮的前景色(这是依赖属性)
/// </summary>
public static readonly DependencyProperty MouserOverForegroundProperty =
DependencyProperty.Register ( "MouserOverForeground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.Black ) ); /// <summary>
/// 鼠标移到按钮上时,按钮的背景色(这是依赖属性)
/// </summary>
public static readonly DependencyProperty MouseOverBackgroundProperty =
DependencyProperty.Register ( "MouseOverBackground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.White ) ); /// <summary>
/// 当鼠标按下时,按钮的前景色(这是依赖属性)
/// </summary>
public static readonly DependencyProperty MousedownForegroundProperty =
DependencyProperty.Register ( "MousedownForeground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.Black ) ); /// <summary>
/// 当鼠标按下时,按钮的背景色(这是依赖属性)
/// </summary>
public static readonly DependencyProperty MousedownBackgroundProperty =
DependencyProperty.Register ( "MousedownBackground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.White ) ); /// <summary>
/// 当按钮不可用时,按钮的前景色(这是依赖属性)
/// </summary>
public static readonly DependencyProperty DisabledForegroundProperty =
DependencyProperty.Register ( " DisabledForeground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.Black ) ); /// <summary>
/// 当按钮不可用时,按钮的背景色(这是依赖属性)
/// </summary>
public static readonly DependencyProperty DisabledBackgroundProperty =
DependencyProperty.Register ( "DisabledBackground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.White ) );
#endregion

然后更改按钮的样式,样式封装在资源字典中:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Zmy.Wpf.Controls">
<Style x:Key="TextButtonStyle" TargetType="{x:Type local:TextButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TextButton}">
<Border x:Name="buttonBorder"
Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Foreground}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter> <Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=MouserOverForeground}"/>
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverBackground}"/>
</Trigger> <Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=MousedownForeground}"/>
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=MousedownBackground}"/>
</Trigger> <Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=DisabledForeground}"/>
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=DisabledBackground}"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>

然后在TextButton的构造函数中设置按钮的样式:

#region 构造函数
public TextButton() : base()
{
//获取资源文件信息
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri ( "/Zmy.Wpf.Controls;component/Style.xaml", UriKind.Relative );
this.Resources.MergedDictionaries.Add ( rd );
//设置样式
this.Style = this.FindResource ( "TextButtonStyle" ) as Style;
}
#endregion

这样整个文字按钮就封装好了,调用起来非常简单:

        <controls:TextButton Content="测试按钮" Width="300" Height="50"
MouserOverForeground="Yellow" MouseOverBackground="Blue" MousedownBackground="Green" MousedownForeground="Blue"/> <controls:TextButton Content="测试按钮" Width="300" Height="50" IsEnabled="False"
DisabledForeground="Yellow" DisabledBackground="Blue" Margin="0,100,0,0"/>

源代码下载:http://download.csdn.net/detail/lyclovezmy/7356125

不要积分。

对应的图片按钮封装:http://www.cnblogs.com/DoNetCoder/p/3732310.html

WPF控件库:文字按钮的封装的更多相关文章

  1. 国内开源C# WPF控件库Panuon.UI.Silver推荐

    国内优秀的WPF开源控件库,Panuon.UI的优化版本.一个漂亮的.使用样式与附加属性的WPF UI控件库,值得向大家推荐使用与学习. 今天站长(Dotnet9,站长网址:https://dotne ...

  2. 国内开源C# WPF控件库Panuon.UI.Silver强力推荐

    国内优秀的WPF开源控件库,Panuon.UI的优化版本.一个漂亮的.使用样式与附加属性的WPF UI控件库,值得向大家推荐使用与学习. 今天站长(Dotnet9,站长网址:https://dotne ...

  3. 《Dotnet9》系列-开源C# WPF控件库3《HandyControl》强力推荐

    大家好,我是Dotnet9小编,一个从事dotnet开发8年+的程序员.我最近开始写dotnet分享文章,希望能让更多人看到dotnet的发展,了解更多dotnet技术,帮助dotnet程序员应用do ...

  4. 《Dotnet9》系列-开源C# WPF控件库2《Panuon.UI.Silver》强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  5. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程

    反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)   背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...

  6. 《Dotnet9》系列-开源C# WPF控件库1《MaterialDesignInXAML》强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  7. WPF 控件库——仿制Chrome的ColorPicker

    WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...

  8. (四)开源C# WPF控件库《AduSkin – UI》

    微信公众号:[Dotnet9的博客],网站:[Dotnet9],问题或建议:[请网站留言], 如果对您有所帮助:[欢迎赞赏]. 开源C# WPF控件库系列: (一)开源C# WPF控件库<Mat ...

  9. 我的WPF控件库——KAN.WPF.XCtrl(141105)

    自己开发的WPF控件库,只是初版,有扩展的Button,TextBox,Window.详细参见前几篇博文. WPF自定义控件(一)——Button:http://www.cnblogs.com/Qin ...

  10. WPF 控件库——仿制Windows10的进度条

    WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...

随机推荐

  1. preg_replace的一些细节

    .$pattern是数组,$replace也是数组,则中对应的 元素进行替换 php preg_replace有五个参数,有三个是必须参数 Preg_replace(mixed $pattern, m ...

  2. MySQL(5)---锁

    锁 一概述 数据库锁定机制简单来说,就是数据库为了保证数据的一致性,而使各种共享资源在被并发访问变得有序所设计的一种规则.对于任何一种数据库来说都需要有相应的锁定机制. MySQL各存储引擎使用了三种 ...

  3. hibernate框架(3)---持久化对象

    持久化对象 再讲持久化对象之前,我们先来理解有关session中get方法与 load方法区别: 简单总结: (1)如果你使用load方法,hibernate认为该id对应的对象(数据库记录)在数据库 ...

  4. JavaWeb 并发:FOR UPDATE 实战,监测并解决。

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket 一.前言 针对并发,老生常谈了.目前一个通用的做法有两种:锁机制:1.悲观锁:2.乐观锁. 但是这篇我 ...

  5. JVM读书笔记之OOM

    在Java虚拟机规范的描述中,除了程序计数器外,虚拟机内存的其他几个运行时区域都有发生OutOfMemoryError(OOM)异常的可能,本文总结了若干实例来验证异常及发生的场景. 下文代码的开头都 ...

  6. IDEA添加Git项目

    1.进入主页面IntelliJ IDEA (如果不知道如何进入主页面,在加载项目是点击“Cancel”按钮 ) 2.点击“Check out from Version Control” ,选择“Git ...

  7. mysql 更新语句中加判断条件

    UPDATE loan_overdue_list l setl.type_status=(CASE WHEN l.overdue_days>(select c.overdue_one from ...

  8. 聊聊数据库~2.SQL环境篇

    传统数据库 上篇文章:聊聊数据库~开篇 https://www.cnblogs.com/dotnetcrazy/p/9690466.html 本来准备直接开讲NoSQL的(当时开篇就是说的NoSQL) ...

  9. ife 零基础学院 day 2

    第二天:给自己做一个在线简历吧 最后的验证,提出了几个问题,尝试解答一下 HTML是什么,HTML5是什么 HTML的定义摘抄自w3school的HTML 简介 HTML 是用来描述网页的一种语言. ...

  10. Try Catch 嵌套问题

    程序错误 问题描述: 在一个事物中,插入两张表数据,但是第一个成功,第二个失败了,没有起到所谓的事物的功能,这让我百思不得其解 问题所在: 本质上其实报错了,但是错误被吃掉了,具体来说,就是 try ...