WPF——给button添加背景图片

曾经终败给现在 2024-04-06 10:25 197阅读 0赞

只是想做一个很简单的图片按钮而已,不需要那么复杂。

=============================================================================================

在WPF中,如果要想给按钮控件Button加上图片,最直接的做法是修改控件模板,在模板中加入想要的图片,代码如下图所示:

复制代码

复制代码

  1. 但是这样做有一个弊端——每次需要用到图片按钮的时候都要去修改模板。因为上面的示例代码中,模板代码过于精简,所以乍看之下似乎这种做法也没有什么不好。但是在实际的应用中,按钮控件的模板往往复杂得多,比如,有很多的Trigger事件,往往需要根据鼠标或按钮的状态来调整控件的图片、字体、背景等状态。因此,如果每次应用图片控件的时候都修改模板,很可能会导致xaml文件的代码量爆炸。
  2. 先给出一个简单的MVVM的绑定方式,供大家参考吧,后面会给出控件重写的方案。

复制代码

下面是控件重写的方案:
一个可行的解决方案为,封装一个用于图片按钮的自定义按钮控件,该控件继承自Button控件,但是额外增加了一些用户图片绑定的依赖属性,同时在控件的默认外观模板中,通过TemplateBinding的方式绑定到依赖属性上,这样在使用的时候便可以直接通过绑定的方式设置图片按钮需要显示的图片,不再需要修改控件模板。

  1. 其实现方式如下:
  2. 代码结构
  3. 如图所示,

采用自定义控件(CustomControl)的方式对Button控件进行封装。其中ImageButton.xaml为默认控件模板,ImageButton.cs为控件的逻辑控制文件,其中包含了ImageButton控件所需要的新的依赖属性,包括图片源属性等。

  1. 模板代码

复制代码

  1. <Style TargetType="{x:Type local:ImageButton}">
  2. <Setter Property="Cursor" Value="Hand"/>
  3. <Setter Property="Template">
  4. <Setter.Value>
  5. <ControlTemplate TargetType="{x:Type local:ImageButton}">
  6. <Border Background="{TemplateBinding Background}"
  7. BorderBrush="{TemplateBinding BorderBrush}"
  8. BorderThickness="{TemplateBinding BorderThickness}">
  9. <Grid x:Name="grid" Background="{TemplateBinding Background}">
  10. <Border x:Name="PART_Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}"/>
  11. <Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
  12. <StackPanel HorizontalAlignment="Center" Orientation="{TemplateBinding IconContentOrientation}" VerticalAlignment="Center" Margin="{TemplateBinding Padding}">
  13. <Grid HorizontalAlignment="Center" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
  14. <Image x:Name="PART_Icon" Source="{TemplateBinding Icon}" Height="{TemplateBinding IconHeight}" Width="{TemplateBinding IconWidth}"/>
  15. <Image x:Name="PART_MouseOverIcon" Visibility="Collapsed" Source="{TemplateBinding IconMouseOver}" Height="{TemplateBinding IconHeight}" Width="{TemplateBinding IconWidth}"/>
  16. <Image x:Name="PART_PressIcon" Visibility="Collapsed" Source="{TemplateBinding IconPress}" Height="{TemplateBinding IconHeight}" Width="{TemplateBinding IconWidth}"/>
  17. </Grid>
  18. <TextBlock x:Name="PART_Content" Text="{TemplateBinding Content}"
  19. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
  20. VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
  21. SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
  22. Margin="{TemplateBinding IconContentMargin}"
  23. Foreground="{TemplateBinding Foreground}"
  24. TextTrimming="CharacterEllipsis"/>
  25. </StackPanel>
  26. </Grid>
  27. </Grid>
  28. </Border>
  29. <ControlTemplate.Triggers>
  30. <Trigger Property="IsMouseOver" Value="True">
  31. <Setter Property="Foreground" TargetName="PART_Content" Value="{Binding MouseOverForeground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>
  32. <Setter Property="Background" TargetName="PART_Border" Value="{Binding MouseOverBackground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>
  33. <Setter Property="BorderBrush" TargetName="PART_Border" Value="{Binding MouseOverBorderBrush,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>
  34. <Setter Property="Visibility" TargetName="PART_MouseOverIcon" Value="Visible"/>
  35. <Setter Property="Visibility" TargetName="PART_Icon" Value="Collapsed"/>
  36. <Setter Property="Visibility" TargetName="PART_PressIcon" Value="Collapsed"/>
  37. </Trigger>
  38. <Trigger Property="IsPressed" Value="True">
  39. <Setter Property="Foreground" TargetName="PART_Content" Value="{Binding MouseDownForeground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>
  40. <Setter Property="Background" TargetName="PART_Border" Value="{Binding MouseDownBackground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>
  41. <Setter Property="BorderBrush" TargetName="PART_Border" Value="{Binding MouseDownBorderBrush,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ImageButton}}}"/>
  42. <Setter Property="Visibility" TargetName="PART_PressIcon" Value="Visible"/>
  43. <Setter Property="Visibility" TargetName="PART_Icon" Value="Collapsed"/>
  44. <Setter Property="Visibility" TargetName="PART_MouseOverIcon" Value="Collapsed"/>
  45. </Trigger>
  46. <Trigger Property="IsEnabled" Value="False">
  47. <Setter Property="Opacity" Value="0.5" />
  48. </Trigger>
  49. <Trigger Property="Text" SourceName="PART_Content" Value="">
  50. <Setter Property="Visibility" TargetName="PART_Content" Value="Collapsed"/>
  51. </Trigger>
  52. <Trigger Property="Text" SourceName="PART_Content" Value="{x:Null}">
  53. <Setter Property="Visibility" TargetName="PART_Content" Value="Collapsed"/>
  54. </Trigger>
  55. </ControlTemplate.Triggers>
  56. </ControlTemplate>
  57. </Setter.Value>
  58. </Setter>
  59. </Style>

复制代码

  1. 在模板中,通过TemplateBinding 的方式绑定了控件中的自定义属性,并默认显示给定的图标和文字。
  2. 然后通过触发器,当鼠标悬停或按下的时候,控制相关图标的显示隐藏以及文字的背景色、前景色和边框颜色。
  3. 自定义依赖属性
  4. ImageButton.cs中定义依赖属性,这些依赖属性包含了图片按钮控件的边框、前景色、背景色,图片等属性。在复写的OnApplyTemplate方法中,会判断如果某些依赖属性的值为null,则使用默认属性。

复制代码
public class ImageButton : Button
{
static ImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
}

  1. public override void OnApplyTemplate()
  2. {
  3. base.OnApplyTemplate();
  4. if (this.MouseOverBackground == null)
  5. {
  6. this.MouseOverBackground = Background;
  7. }
  8. if (this.MouseDownBackground == null)
  9. {
  10. if (this.MouseOverBackground == null)
  11. {
  12. this.MouseDownBackground = Background;
  13. }
  14. else
  15. {
  16. this.MouseDownBackground = MouseOverBackground;
  17. }
  18. }
  19. if (this.MouseOverBorderBrush == null)
  20. {
  21. this.MouseOverBorderBrush = BorderBrush;
  22. }
  23. if (this.MouseDownBorderBrush == null)
  24. {
  25. if (this.MouseOverBorderBrush == null)
  26. {
  27. this.MouseDownBorderBrush = BorderBrush;
  28. }
  29. else
  30. {
  31. this.MouseDownBorderBrush = MouseOverBorderBrush;
  32. }
  33. }
  34. if (this.MouseOverForeground == null)
  35. {
  36. this.MouseOverForeground = Foreground;
  37. }
  38. if (this.MouseDownForeground == null)
  39. {
  40. if (this.MouseOverForeground == null)
  41. {
  42. this.MouseDownForeground = Foreground;
  43. }
  44. else
  45. {
  46. this.MouseDownForeground = this.MouseOverForeground;
  47. }
  48. }
  49. }
  50. #region Dependency Properties
  51. /// <summary>
  52. /// 鼠标移上去的背景颜色
  53. /// </summary>
  54. public static readonly DependencyProperty MouseOverBackgroundProperty
  55. = DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(ImageButton));
  56. /// <summary>
  57. /// 鼠标按下去的背景颜色
  58. /// </summary>
  59. public static readonly DependencyProperty MouseDownBackgroundProperty
  60. = DependencyProperty.Register("MouseDownBackground", typeof(Brush), typeof(ImageButton));
  61. /// <summary>
  62. /// 鼠标移上去的字体颜色
  63. /// </summary>
  64. public static readonly DependencyProperty MouseOverForegroundProperty
  65. = DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));
  66. /// <summary>
  67. /// 鼠标按下去的字体颜色
  68. /// </summary>
  69. public static readonly DependencyProperty MouseDownForegroundProperty
  70. = DependencyProperty.Register("MouseDownForeground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));
  71. /// <summary>
  72. /// 鼠标移上去的边框颜色
  73. /// </summary>
  74. public static readonly DependencyProperty MouseOverBorderBrushProperty
  75. = DependencyProperty.Register("MouseOverBorderBrush", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));
  76. /// <summary>
  77. /// 鼠标按下去的边框颜色
  78. /// </summary>
  79. public static readonly DependencyProperty MouseDownBorderBrushProperty
  80. = DependencyProperty.Register("MouseDownBorderBrush", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));
  81. /// <summary>
  82. /// 圆角
  83. /// </summary>
  84. public static readonly DependencyProperty CornerRadiusProperty
  85. = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ImageButton), null);
  86. //图标
  87. public static readonly DependencyProperty IconProperty
  88. = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(ImageButton), null);
  89. //鼠标移上去的图标图标
  90. public static readonly DependencyProperty IconMouseOverProperty
  91. = DependencyProperty.Register("IconMouseOver", typeof(ImageSource), typeof(ImageButton), null);
  92. //鼠标按下去的图标图标
  93. public static readonly DependencyProperty IconPressProperty
  94. = DependencyProperty.Register("IconPress", typeof(ImageSource), typeof(ImageButton), null);
  95. //图标高度
  96. public static readonly DependencyProperty IconHeightProperty
  97. = DependencyProperty.Register("IconHeight", typeof(double), typeof(ImageButton), new PropertyMetadata(24.0, null));
  98. //图标宽度
  99. public static readonly DependencyProperty IconWidthProperty
  100. = DependencyProperty.Register("IconWidth", typeof(double), typeof(ImageButton), new PropertyMetadata(24.0, null));
  101. //图标和内容的对齐方式
  102. public static readonly DependencyProperty IconContentOrientationProperty
  103. = DependencyProperty.Register("IconContentOrientation", typeof(Orientation), typeof(ImageButton), new PropertyMetadata(Orientation.Horizontal, null));
  104. //图标和内容的距离
  105. public static readonly DependencyProperty IconContentMarginProperty
  106. = DependencyProperty.Register("IconContentMargin", typeof(Thickness), typeof(ImageButton), new PropertyMetadata(new Thickness(0, 0, 0, 0), null));
  107. #endregion
  108. #region Property Wrappers
  109. public Brush MouseOverBackground
  110. {
  111. get
  112. {
  113. return (Brush)GetValue(MouseOverBackgroundProperty);
  114. }
  115. set { SetValue(MouseOverBackgroundProperty, value); }
  116. }
  117. public Brush MouseDownBackground
  118. {
  119. get
  120. {
  121. return (Brush)GetValue(MouseDownBackgroundProperty);
  122. }
  123. set { SetValue(MouseDownBackgroundProperty, value); }
  124. }
  125. public Brush MouseOverForeground
  126. {
  127. get
  128. {
  129. return (Brush)GetValue(MouseOverForegroundProperty);
  130. }
  131. set { SetValue(MouseOverForegroundProperty, value); }
  132. }
  133. public Brush MouseDownForeground
  134. {
  135. get
  136. {
  137. return (Brush)GetValue(MouseDownForegroundProperty);
  138. }
  139. set { SetValue(MouseDownForegroundProperty, value); }
  140. }
  141. public Brush MouseOverBorderBrush
  142. {
  143. get { return (Brush)GetValue(MouseOverBorderBrushProperty); }
  144. set { SetValue(MouseOverBorderBrushProperty, value); }
  145. }
  146. public Brush MouseDownBorderBrush
  147. {
  148. get { return (Brush)GetValue(MouseDownBorderBrushProperty); }
  149. set { SetValue(MouseDownBorderBrushProperty, value); }
  150. }
  151. public CornerRadius CornerRadius
  152. {
  153. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  154. set { SetValue(CornerRadiusProperty, value); }
  155. }
  156. public ImageSource Icon
  157. {
  158. get { return (ImageSource)GetValue(IconProperty); }
  159. set { SetValue(IconProperty, value); }
  160. }
  161. public ImageSource IconMouseOver
  162. {
  163. get { return (ImageSource)GetValue(IconMouseOverProperty); }
  164. set { SetValue(IconMouseOverProperty, value); }
  165. }
  166. public ImageSource IconPress
  167. {
  168. get { return (ImageSource)GetValue(IconPressProperty); }
  169. set { SetValue(IconPressProperty, value); }
  170. }
  171. public double IconHeight
  172. {
  173. get { return (double)GetValue(IconHeightProperty); }
  174. set { SetValue(IconHeightProperty, value); }
  175. }
  176. public double IconWidth
  177. {
  178. get { return (double)GetValue(IconWidthProperty); }
  179. set { SetValue(IconWidthProperty, value); }
  180. }
  181. public Orientation IconContentOrientation
  182. {
  183. get { return (Orientation)GetValue(IconContentOrientationProperty); }
  184. set { SetValue(IconContentOrientationProperty, value); }
  185. }
  186. public Thickness IconContentMargin
  187. {
  188. get { return (Thickness)GetValue(IconContentMarginProperty); }
  189. set { SetValue(IconContentMarginProperty, value); }
  190. }
  191. #endregion

}
复制代码

  1. 控件的应用
  2. 应用控件的时候,只需要简单的绑定控件的相关属性即可。

发表评论

表情:
评论列表 (有 0 条评论,197人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C# PDF添加图片背景

    今天要实现的是给PDF文件添加图片背景这个功能。PDF是近年来最流行的文件之一,无论是办公还是日常生活中都经常会用到,很多时候,PDF文件的背景色都是白色,看多了难免觉得累,更

    相关 MFC 对话框添加图片背景

    在windows开发当中做界面的主要技术之一就是使用MFC,通常我们看到的QQ,360,暴风影音这些漂亮的界面都可以用MFC来实现。今天我们来说一下如何用MFC美化对话框,默认