ImageButton.cs 9.1 KB

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