翼度科技»论坛 编程开发 .net 查看内容

WPF 实现图标按钮

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
假设需要实现一个图标和文本结合的按钮 ,普通做法是 直接重写该按钮的模板;
如果想作为通用的呢?
两种做法:

  • 附加属性
  • 自定义控件
推荐使用附加属性的形式
第一种:附加属性

创建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     
  6. 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>
复制代码
效果

 
第二种:自定义控件

后续更新
 

来源:https://www.cnblogs.com/fengxinyuan/p/18295339
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具