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

实现:继承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

最新文章

  1. Java在处理大数据的时候一些小技巧
  2. EF CodeFirst 如何通过配置自动创建数据库&lt;当模型改变时&gt;
  3. kuangbin_ShortPath R (HDU 4370)
  4. Python修饰器的函数式编程
  5. 利用SOLR搭建企业搜索平台 之——solr配置solrconfig.xml
  6. iOS 使用COPY声明NSSTRING属性
  7. codevs 1106 篝火晚会
  8. hdu2571命
  9. MKNetworkKit 使用
  10. 《JS权威指南学习总结--8.5 作为命名空间的函数》
  11. Codeforces Round #454 D. Seating of Students
  12. Android群英传笔记——第二章:Android开发工具新接触
  13. 利用异或求(整数数组中,有2K+1个数,其中有2k个相同,找出不相同的那个数)
  14. jexus独立版设置支持https
  15. springmvc解决中文乱码问题
  16. .net core通过发布nuget实现引用项目
  17. pillow生成验证码
  18. 为什么使用this构造器
  19. C++用new来创建对象和非new来创建对象的区别
  20. zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法

热门文章

  1. 使用 Docker 部署 Grafana + Prometheus 监控 MySQL 数据库
  2. 微信小程序入门(二)
  3. 短视频 SDK 6大功能技术实现方式详解
  4. 【WebAPI】从零开始学会使用.NET Core WebAPI
  5. vue 项目实战 (vue全家桶之--- vuex)
  6. springBoot(5)---单元测试,全局异常
  7. Android中设置控件的背景颜色的方式整理
  8. ftp服务器搭建及简单操作
  9. Redis 超时排查
  10. 网络编程第三讲UDP编写