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

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

  1. #region 依赖属性
  2. /// <summary>
  3. /// 当鼠标移到按钮上时,按钮的前景色(这是依赖属性)
  4. /// </summary>
  5. public static readonly DependencyProperty MouserOverForegroundProperty =
  6. DependencyProperty.Register ( "MouserOverForeground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.Black ) );
  7.  
  8. /// <summary>
  9. /// 鼠标移到按钮上时,按钮的背景色(这是依赖属性)
  10. /// </summary>
  11. public static readonly DependencyProperty MouseOverBackgroundProperty =
  12. DependencyProperty.Register ( "MouseOverBackground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.White ) );
  13.  
  14. /// <summary>
  15. /// 当鼠标按下时,按钮的前景色(这是依赖属性)
  16. /// </summary>
  17. public static readonly DependencyProperty MousedownForegroundProperty =
  18. DependencyProperty.Register ( "MousedownForeground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.Black ) );
  19.  
  20. /// <summary>
  21. /// 当鼠标按下时,按钮的背景色(这是依赖属性)
  22. /// </summary>
  23. public static readonly DependencyProperty MousedownBackgroundProperty =
  24. DependencyProperty.Register ( "MousedownBackground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.White ) );
  25.  
  26. /// <summary>
  27. /// 当按钮不可用时,按钮的前景色(这是依赖属性)
  28. /// </summary>
  29. public static readonly DependencyProperty DisabledForegroundProperty =
  30. DependencyProperty.Register ( " DisabledForeground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.Black ) );
  31.  
  32. /// <summary>
  33. /// 当按钮不可用时,按钮的背景色(这是依赖属性)
  34. /// </summary>
  35. public static readonly DependencyProperty DisabledBackgroundProperty =
  36. DependencyProperty.Register ( "DisabledBackground", typeof ( Brush ), typeof ( TextButton ), new PropertyMetadata ( Brushes.White ) );
  37. #endregion

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

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:local="clr-namespace:Zmy.Wpf.Controls">
  4. <Style x:Key="TextButtonStyle" TargetType="{x:Type local:TextButton}">
  5. <Setter Property="Template">
  6. <Setter.Value>
  7. <ControlTemplate TargetType="{x:Type local:TextButton}">
  8. <Border x:Name="buttonBorder"
  9. Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Foreground}">
  10. <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
  11. </Border>
  12. </ControlTemplate>
  13. </Setter.Value>
  14. </Setter>
  15.  
  16. <Style.Triggers>
  17. <Trigger Property="IsMouseOver" Value="True">
  18. <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=MouserOverForeground}"/>
  19. <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=MouseOverBackground}"/>
  20. </Trigger>
  21.  
  22. <Trigger Property="IsPressed" Value="True">
  23. <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=MousedownForeground}"/>
  24. <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=MousedownBackground}"/>
  25. </Trigger>
  26.  
  27. <Trigger Property="IsEnabled" Value="False">
  28. <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=DisabledForeground}"/>
  29. <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=DisabledBackground}"/>
  30. </Trigger>
  31. </Style.Triggers>
  32. </Style>
  33. </ResourceDictionary>

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

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

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

  1. <controls:TextButton Content="测试按钮" Width="300" Height="50"
  2. MouserOverForeground="Yellow" MouseOverBackground="Blue" MousedownBackground="Green" MousedownForeground="Blue"/>
  3.  
  4. <controls:TextButton Content="测试按钮" Width="300" Height="50" IsEnabled="False"
  5. 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. Ocelot简易教程(二)之快速开始2

    为什么这篇的标题叫"Ocelot简易教程(二)之快速开始2"呢,因为很多朋友跟我说上一篇" Ocelot简易教程(二)之快速开始1"内容太少了,只是简单介绍Oc ...

  2. python 2解决编码问题

    import sys reload(sys) sys.setdefaultencoding('utf-8') 另:python 3的open函数可以直接加encoding参数

  3. Kubernetes 服务入口管理 Traefik Ingress Controller

    前面部署了 kubernetes/ingress-nginx 作为 Ingress Controller,使用 Nginx 反向代理与负载,通过 Ingress Controller 不断的跟 Kub ...

  4. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  5. Java 容器源码分析之 Set

    Set 表示由无重复对象组成的集合,也是集合框架中重要的一种集合类型,直接扩展自 Collection 接口.在一个 Set 中,不能有两个引用指向同一个对象,或两个指向 null 的引用.如果对象 ...

  6. springboot情操陶冶-@Conditional和@AutoConfigureAfter注解解析

    承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@AutoConfigureAfter和@Conditional注解的作用与解析 1.@Condit ...

  7. 【测试工程师面试】在BOSS直聘上和面试官的一问一答

    岗位描述: 信用卡核心系统功能测试,负责测试计划制定,测试设计,测试执行,测试进度掌控,自动化工具建设等工作.有责任心,执行力强,工作认真细致,逻辑思维强熟悉linux,oracle或者IBM大型机操 ...

  8. PXE+kickstart无人值守安装CentOS 7

    kickstart+cobbler系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 本文是PXE+kickstart无人值守安装CentOS ...

  9. Ubuntu16---安装mysql5.7未提示输入密码,安装后修改mysql密码默认密码

    Ubuntu16安装mysql5.7未提示输入密码,安装后修改mysql密码默认密码 mysql默认密码为空 但是使用mysql -uroot -p 命令连接mysql时,报错 ERROR 1045 ...

  10. 第一册:lesson twentynine..

    原文:Come in ,Amy. A:Come in B. Shut the door,please. This bedroom's very untidy. B:What must I do Mrs ...