假设需要实现一个图标和文本结合的按钮 ,普通做法是 直接重写该按钮的模板;

如果想作为通用的呢?

两种做法:

  1. 附加属性
  2. 自定义控件

推荐使用附加属性的形式

第一种:附加属性

创建Button的附加属性  ButtonExtensions

  1. 1 public static class ButtonExtensions
  2. 2 {
  3. 3 // Using a DependencyProperty as the backing store for IconWidth. This enables animation, styling, binding, etc...
  4. 4 public static readonly DependencyProperty IconWidthProperty =
  5. 5 DependencyProperty.RegisterAttached("IconWidth", typeof(int), typeof(ButtonExtensions), new PropertyMetadata(0));
  6. 6
  7. 7 public static int GetIconWidth(DependencyObject obj)
  8. 8 {
  9. 9 return (int)obj.GetValue(IconWidthProperty);
  10. 10 }
  11. 11
  12. 12 public static void SetIconWidth(DependencyObject obj, int value)
  13. 13 {
  14. 14 obj.SetValue(IconWidthProperty, value);
  15. 15 }
  16. 16
  17. 17 // Using a DependencyProperty as the backing store for IconHeight. This enables animation, styling, binding, etc...
  18. 18 public static readonly DependencyProperty IconHeightProperty =
  19. 19 DependencyProperty.RegisterAttached("IconHeight", typeof(int), typeof(ButtonExtensions), new PropertyMetadata(0));
  20. 20
  21. 21 public static int GetIconHeight(DependencyObject obj)
  22. 22 {
  23. 23 return (int)obj.GetValue(IconHeightProperty);
  24. 24 }
  25. 25
  26. 26 public static void SetIconHeight(DependencyObject obj, int value)
  27. 27 {
  28. 28 obj.SetValue(IconHeightProperty, value);
  29. 29 }
  30. 30
  31. 31 // Using a DependencyProperty as the backing store for IconGeometry. This enables animation, styling, binding, etc...
  32. 32 public static readonly DependencyProperty IconGeometryProperty =
  33. 33 DependencyProperty.RegisterAttached("IconGeometry", typeof(Geometry), typeof(ButtonExtensions), new PropertyMetadata((object)null));
  34. 34
  35. 35 public static Geometry GetIconGeometry(DependencyObject obj)
  36. 36 {
  37. 37 return (Geometry)obj.GetValue(IconGeometryProperty);
  38. 38 }
  39. 39
  40. 40 public static void SetIconGeometry(DependencyObject obj, Geometry value)
  41. 41 {
  42. 42 obj.SetValue(IconGeometryProperty, value);
  43. 43 }
  44. 44
  45. 45 }

样式

  1. 1 <ResourceDictionary
  2. 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. 4 xmlns:coreHelper="clr-namespace:NeonGenesis.Core.AttachedProperties;assembly=NeonGenesis.Core">
  5. 5 <Style x:Key="ButtonVerBase" TargetType="{x:Type Button}">
  6. 6 <Setter Property="BorderThickness" Value="0" />
  7. 7 <Setter Property="HorizontalContentAlignment" Value="Center" />
  8. 8 <Setter Property="VerticalContentAlignment" Value="Center" />
  9. 9 <Setter Property="Padding" Value="10,5" />
  10. 10 <Setter Property="FrameworkElement.Cursor" Value="Hand" />
  11. 11 <Setter Property="UIElement.SnapsToDevicePixels" Value="True" />
  12. 12 <Setter Property="coreHelper:ButtonExtensions.IconHeight" Value="24" />
  13. 13 <Setter Property="coreHelper:ButtonExtensions.IconWidth" Value="24" />
  14. 14 <Setter Property="Template">
  15. 15 <Setter.Value>
  16. 16 <ControlTemplate TargetType="{x:Type ButtonBase}">
  17. 17 <Border
  18. 18 Name="border"
  19. 19 Background="{TemplateBinding Background}"
  20. 20 BorderBrush="{TemplateBinding BorderBrush}"
  21. 21 BorderThickness="{TemplateBinding BorderThickness}"
  22. 22 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
  23. 23 <Grid>
  24. 24 <StackPanel
  25. 25 Margin="{TemplateBinding Padding}"
  26. 26 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
  27. 27 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
  28. 28 Orientation="Vertical">
  29. 29 <Path
  30. 30 Name="pathIcon"
  31. 31 Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(coreHelper:ButtonExtensions.IconWidth)}"
  32. 32 Height="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(coreHelper:ButtonExtensions.IconHeight)}"
  33. 33 Margin="0,0,0,5"
  34. 34 Data="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(coreHelper:ButtonExtensions.IconGeometry)}"
  35. 35 Fill="{TemplateBinding Foreground}"
  36. 36 Stretch="Uniform" />
  37. 37 <ContentPresenter
  38. 38 Name="contentPresenter"
  39. 39 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
  40. 40 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
  41. 41 Focusable="False"
  42. 42 RecognizesAccessKey="True"
  43. 43 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
  44. 44 </StackPanel>
  45. 45 </Grid>
  46. 46 </Border>
  47. 47 <ControlTemplate.Triggers>
  48. 48 <Trigger Property="coreHelper:ButtonExtensions.IconGeometry" Value="{x:Null}">
  49. 49 <Setter TargetName="pathIcon" Property="Visibility" Value="Collapsed" />
  50. 50 </Trigger>
  51. 51 <Trigger Property="Content" Value="{x:Null}">
  52. 52 <Setter TargetName="pathIcon" Property="Margin" Value="0" />
  53. 53 </Trigger>
  54. 54 </ControlTemplate.Triggers>
  55. 55 </ControlTemplate>
  56. 56 </Setter.Value>
  57. 57 </Setter>
  58. 58 </Style>
  59. 59 </ResourceDictionary>

使用示例

  1. 1 <Button
  2. 2 Width="80"
  3. 3 Height="80"
  4. 4 coreHelper:ButtonExtensions.IconGeometry="{StaticResource RunningGeometry}"
  5. 5 coreHelper:ButtonExtensions.IconHeight="40"
  6. 6 coreHelper:ButtonExtensions.IconWidth="40"
  7. 7 Background="#1e90ff"
  8. 8 Content="运行"
  9. 9 Foreground="White"
  10. 10 Style="{StaticResource ButtonVerBase}" />

RunningGeometry为

  1. <PathGeometry x:Key="RunningGeometry">M41.355947 0h572.962133a41.355947 41.355947 0 0 1 41.355947 41.355947v100.037973H0V41.355947A41.355947 41.355947 0 0 1 41.355947 0zM0 210.356907v772.287146A41.355947 41.355947 0 0 0 41.355947 1024h941.288106A41.355947 41.355947 0 0 0 1024 982.644053V210.356907z m851.88608 295.867733L581.973333 776.137387a47.786667 47.786667 0 0 1-66.710186 0.832853 47.786667 47.786667 0 0 1-7.796054-6.294187l-115.083946-115.0976-120.54528 120.558934a47.786667 47.786667 0 0 1-67.611307 0 47.786667 47.786667 0 0 1 0-67.611307l147.12832-147.12832a48.237227 48.237227 0 0 1 13.653333-9.557333 47.786667 47.786667 0 0 1 62.887254 4.096l119.6032 119.507626 236.776106-236.817066a47.786667 47.786667 0 0 1 67.611307 0 47.786667 47.786667 0 0 1 0 67.597653z</PathGeometry>

效果

第二种:自定义控件

后续更新

WPF 实现图标按钮的更多相关文章

  1. WPF实现Twitter按钮效果

    最近上网看到这个CSS3实现的Twitter按钮,感觉很漂亮,于是想用WPF来实现下. 实现这个效果,参考了CSS3 原文地址:http://www.html5tricks.com/css3-twit ...

  2. 添加“返回顶部”小图标按钮的JS(JavaScript)代码详解

    如何给自己的网站添加方便快捷的"返回顶部"小图标按钮呢?如下图: JS源代码: /** * JavaScript脚本实现回到页面顶部示例 * @param acceleration ...

  3. WPF 带清除按钮的文字框SearchTextBox

    原文:WPF 带清除按钮的文字框SearchTextBox 基于TextBox的带清除按钮的搜索框 样式部分: <!--带清除按钮文字框--> <Style TargetType=& ...

  4. wpf 状态栏图标背景闪烁提醒 FlashWindowEx

    原文:wpf 状态栏图标背景闪烁提醒 FlashWindowEx using System; using System.Runtime.InteropServices; using System.Wi ...

  5. WPF实现Twitter按钮效果(转)

    最近上网看到这个CSS3实现的Twitter按钮,感觉很漂亮,于是想用WPF来实现下. 实现这个效果,参考了CSS3 原文地址:http://www.html5tricks.com/css3-twit ...

  6. 在VS2005中设置WPF中自定义按钮的事件

    原文:在VS2005中设置WPF中自定义按钮的事件 上篇讲了如何在Blend中绘制圆角矩形(http://blog.csdn.net/johnsuna/archive/2007/08/13/17407 ...

  7. WPF字体图标——IconFont

    原文:WPF字体图标--IconFont 版权声明:本文为[CSDN博主:松一160]原创文章,未经允许不得转载. https://blog.csdn.net/songyi160/article/de ...

  8. WPF字体图标——FontAwesom

    原文:WPF字体图标--FontAwesom 版权声明:本文为[CSDN博主:松一160]原创文章,未经允许不得转载. https://blog.csdn.net/songyi160/article/ ...

  9. [原译]一步步教你制作WPF圆形玻璃按钮

    原文:[原译]一步步教你制作WPF圆形玻璃按钮 图1 1.介绍 从我开始使用vista的时候,我就非常喜欢它的圆形玻璃按钮.WPF最好的一个方面就是允许自定义任何控件的样式.用了一段时间的Micros ...

  10. WPF 给Button按钮加小图标图片Image

    前言:当WPF项目后台完成到一定程度的时候,就可以对XAML前端进行美化啦,个人认为XAML前端还是挺有意思的. 下面举一个Button加过小图标后的例子: 是不是比生硬的文字看来更人性化了呢? 不多 ...

随机推荐

  1. python正则表达式替换所有内容并同时保留找到的内容

    除了一些专业的工具,例如ue,大部分编程语言的函数包都挺让人迷惑的,例如Java,js. 因为的确有许多功能是很常用的,但是他们又不提供,非得要程序员自己去实现,或者是利用三方的包. 到底是什么理由了 ...

  2. js-对象创建

    哥被逼得要当全栈工程师,今天练习了下各种对象的创建方式.代码较多参考了https://www.cnblogs.com/ImmortalWang/p/10517091.html 为了方便测试,整合了一个 ...

  3. P6623 [省选联考 2020 A 卷] 树

    day2t2但难度不大,和AGC044C解法类似 题目大意: 给定一棵 \(n\) 个结点的有根树 \(T\),结点从 \(1\) 开始编号,根结点为 \(1\) 号结点,每个结点有一个正整数权值 \ ...

  4. 背包DP——完全背包

    完全背包模型与 0-1 背包类似,与 0-1 背包的区别仅在于一个物品可以选取无限次,而非仅能选取一次. 而状态转移方程于01背包区别在于可以直接从[i][j-w[i]]转移 理由是当我们这样转移时, ...

  5. PySide6之多线程

    一.QThread 方法1:子类化创建多线程 创建子线程,继承自QThread类 在子线程中自定义信号 在子线程中重写 run() 方法,进行信号的触发 在主线程中实例化子线程 在主线程中对子线程的信 ...

  6. NXP i.MX 8M Plus工业核心板硬件说明书( 四核ARM Cortex-A53 + 单核ARM Cortex-M7,主频1.6GHz)

    1          硬件资源 创龙科技SOM-TLIMX8MP是一款基于NXP i.MX 8M Plus的四核ARM Cortex-A53 + 单核ARM Cortex-M7异构多核处理器设计的高端 ...

  7. PowerBuilder现代编程方法X01:PowerPlume的X模式

    临渊羡鱼,不如退而结网. PB现代编程方法X01:PowerPlume的X模式 前言 PowerPlume是PowerBuilder深度创新的扩展开发框架(免费商用). 它不是一个大而全的类库(取决于 ...

  8. .NET周刊【7月第2期 2024-07-14】

    国内文章 开源GTKSystem.Windows.Forms框架让C# winform支持跨平台运行 https://www.cnblogs.com/easywebfactory/p/18289178 ...

  9. 完美卸载Docker

    1,删除docker所在目录 rm -rf /etc/docker rm -rf /run/docker rm -rf /var/lib/dockershim rm -rf /var/lib/dock ...

  10. Visual Studio 必备插件集合:AI 助力开发

     一.前言 2024年AI浪潮席卷全球,编程界迎来全新的挑战与机遇.智能编程.自动化测试.代码审查,这一切都得益于AI技术的迅猛发展,它正在重塑开发者的日常,让编写代码变得更加高效.智能. 精选出最受 ...