WPF控件库:文字按钮的封装
需求:封装按钮,按钮上面只显示文字。在鼠标移上去、鼠标点击按钮、以及将按钮设为不可用时按钮的背景色和前景色需要发生变化
实现:继承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控件库:文字按钮的封装的更多相关文章
- 国内开源C# WPF控件库Panuon.UI.Silver推荐
国内优秀的WPF开源控件库,Panuon.UI的优化版本.一个漂亮的.使用样式与附加属性的WPF UI控件库,值得向大家推荐使用与学习. 今天站长(Dotnet9,站长网址:https://dotne ...
- 国内开源C# WPF控件库Panuon.UI.Silver强力推荐
国内优秀的WPF开源控件库,Panuon.UI的优化版本.一个漂亮的.使用样式与附加属性的WPF UI控件库,值得向大家推荐使用与学习. 今天站长(Dotnet9,站长网址:https://dotne ...
- 《Dotnet9》系列-开源C# WPF控件库3《HandyControl》强力推荐
大家好,我是Dotnet9小编,一个从事dotnet开发8年+的程序员.我最近开始写dotnet分享文章,希望能让更多人看到dotnet的发展,了解更多dotnet技术,帮助dotnet程序员应用do ...
- 《Dotnet9》系列-开源C# WPF控件库2《Panuon.UI.Silver》强力推荐
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程
反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) 背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...
- 《Dotnet9》系列-开源C# WPF控件库1《MaterialDesignInXAML》强力推荐
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- WPF 控件库——仿制Chrome的ColorPicker
WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...
- (四)开源C# WPF控件库《AduSkin – UI》
微信公众号:[Dotnet9的博客],网站:[Dotnet9],问题或建议:[请网站留言], 如果对您有所帮助:[欢迎赞赏]. 开源C# WPF控件库系列: (一)开源C# WPF控件库<Mat ...
- 我的WPF控件库——KAN.WPF.XCtrl(141105)
自己开发的WPF控件库,只是初版,有扩展的Button,TextBox,Window.详细参见前几篇博文. WPF自定义控件(一)——Button:http://www.cnblogs.com/Qin ...
- WPF 控件库——仿制Windows10的进度条
WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...
随机推荐
- 解决删除chrome注册表残留问题
将下面这个全部复制下来并粘贴到命名为“remove.reg”的文件中.双击执行即可 Windows Registry Editor Version 5.00 ;WARNING, this file ...
- Abp + gRpc 如何实现用户会话状态传递
0.背景 在实际项目当中,我们采用的是 Abp 框架,但是 Abp 框架官方并没有针对 Grpc 进行模块封装.基于此我结合 Abp 与 MagicOnion 封装了一个 Abp.Grpc 模块,它包 ...
- tensorflow 1.0 学习:卷积层
在tf1.0中,对卷积层重新进行了封装,比原来版本的卷积层有了很大的简化. 一.旧版本(1.0以下)的卷积函数:tf.nn.conv2d conv2d( input, filter, strides, ...
- HP C7000刀片服务器开关机过程
HP C7000开关机过程 一.HP C7000关机过程 1.关闭台刀片服务器. 2.确认刀片已关机后,登录https://xxx.xxx.xxx.x/,Administrator/2PF2QT, ...
- idea启动多个tomcat失败
Intellij idea中,为在本地调试两个系统之间的调用,配置两个本地tomcat server,设置不同的端口号,如8081和8082,Deploy中加入两个系统各自的Artifact xxx: ...
- PEB标记反调试方法
PEB标记反调试方法 一丶PEB结构简介 PEB.简称进程环境快. 我们在讲DLL隐藏的时候已经说过了. 具体博客链接: https://www.cnblogs.com/iBinary/p/96018 ...
- 如何一步步在生产环境上部署django和vue
本文由云+社区发表 本文主要讲述了如何一步步在生产环境上部署django和vue,操作系统默认为centos 说明:后文中出现的以下字符串均表示具体的路径或者名称,含义如下: DJANGO_DIR-- ...
- Ajax的初步认识
1.背景 2005年,JJG发表了一篇在线文章,介绍了AJAX(Asynchronous Javascript + XML),这项技术能够向服务器请求额外数据而无须卸载页面,说是改变了以前的“单击”, ...
- 第一册:lesson5-6.
原文: A:Good morning. B:Good morning,Mr.A. A:This is Miss C. C is a new student.She is Frech. C ,this ...
- 用Vue.js搭建一个小说阅读网站
目录 1.简介 2.如何使用vue.js 3.部署api服务器 4.vue.js路由配置 5.实现页面加载数据 6.测试vue项目 7.在正式环境部署 8.Vue前端代码下载 1.简介 这是一个使用v ...