ImageButton.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_Office.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 MouseDownBackgroundProperty
  76. = DependencyProperty.Register("MouseDownBackground", typeof(Brush), typeof(ImageButton));
  77. /// <summary>
  78. /// 鼠标移上去的字体颜色
  79. /// </summary>
  80. public static readonly DependencyProperty MouseOverForegroundProperty
  81. = DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));
  82. /// <summary>
  83. /// 鼠标按下去的字体颜色
  84. /// </summary>
  85. public static readonly DependencyProperty MouseDownForegroundProperty
  86. = DependencyProperty.Register("MouseDownForeground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));
  87. /// <summary>
  88. /// 鼠标移上去的边框颜色
  89. /// </summary>
  90. public static readonly DependencyProperty MouseOverBorderBrushProperty
  91. = DependencyProperty.Register("MouseOverBorderBrush", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));
  92. /// <summary>
  93. /// 鼠标按下去的边框颜色
  94. /// </summary>
  95. public static readonly DependencyProperty MouseDownBorderBrushProperty
  96. = DependencyProperty.Register("MouseDownBorderBrush", typeof(Brush), typeof(ImageButton), new PropertyMetadata(null, null));
  97. /// <summary>
  98. /// 圆角
  99. /// </summary>
  100. public static readonly DependencyProperty CornerRadiusProperty
  101. = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ImageButton), null);
  102. //图标
  103. public static readonly DependencyProperty IconProperty
  104. = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(ImageButton), null);
  105. //鼠标移上去的图标图标
  106. public static readonly DependencyProperty IconMouseOverProperty
  107. = DependencyProperty.Register("IconMouseOver", typeof(ImageSource), typeof(ImageButton), null);
  108. //鼠标按下去的图标图标
  109. public static readonly DependencyProperty IconPressProperty
  110. = DependencyProperty.Register("IconPress", typeof(ImageSource), typeof(ImageButton), null);
  111. //图标高度
  112. public static readonly DependencyProperty IconHeightProperty
  113. = DependencyProperty.Register("IconHeight", typeof(double), typeof(ImageButton), new PropertyMetadata(24.0, null));
  114. //图标宽度
  115. public static readonly DependencyProperty IconWidthProperty
  116. = DependencyProperty.Register("IconWidth", typeof(double), typeof(ImageButton), new PropertyMetadata(24.0, null));
  117. //图标和内容的对齐方式
  118. public static readonly DependencyProperty IconContentOrientationProperty
  119. = DependencyProperty.Register("IconContentOrientation", typeof(Orientation), typeof(ImageButton), new PropertyMetadata(Orientation.Horizontal, null));
  120. //图标和内容的距离
  121. public static readonly DependencyProperty IconContentMarginProperty
  122. = DependencyProperty.Register("IconContentMargin", typeof(Thickness), typeof(ImageButton), new PropertyMetadata(new Thickness(0, 0, 0, 0), null));
  123. #endregion
  124. #region 属性包装
  125. public Brush MouseOverBackground
  126. {
  127. get
  128. {
  129. return (Brush)GetValue(MouseOverBackgroundProperty);
  130. }
  131. set { SetValue(MouseOverBackgroundProperty, value); }
  132. }
  133. public Brush MouseDownBackground
  134. {
  135. get
  136. {
  137. return (Brush)GetValue(MouseDownBackgroundProperty);
  138. }
  139. set { SetValue(MouseDownBackgroundProperty, value); }
  140. }
  141. public Brush MouseOverForeground
  142. {
  143. get
  144. {
  145. return (Brush)GetValue(MouseOverForegroundProperty);
  146. }
  147. set { SetValue(MouseOverForegroundProperty, value); }
  148. }
  149. public Brush MouseDownForeground
  150. {
  151. get
  152. {
  153. return (Brush)GetValue(MouseDownForegroundProperty);
  154. }
  155. set { SetValue(MouseDownForegroundProperty, value); }
  156. }
  157. public Brush MouseOverBorderBrush
  158. {
  159. get { return (Brush)GetValue(MouseOverBorderBrushProperty); }
  160. set { SetValue(MouseOverBorderBrushProperty, value); }
  161. }
  162. public Brush MouseDownBorderBrush
  163. {
  164. get { return (Brush)GetValue(MouseDownBorderBrushProperty); }
  165. set { SetValue(MouseDownBorderBrushProperty, value); }
  166. }
  167. public CornerRadius CornerRadius
  168. {
  169. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  170. set { SetValue(CornerRadiusProperty, value); }
  171. }
  172. public ImageSource Icon
  173. {
  174. get { return (ImageSource)GetValue(IconProperty); }
  175. set { SetValue(IconProperty, value); }
  176. }
  177. public ImageSource IconMouseOver
  178. {
  179. get { return (ImageSource)GetValue(IconMouseOverProperty); }
  180. set { SetValue(IconMouseOverProperty, value); }
  181. }
  182. public ImageSource IconPress
  183. {
  184. get { return (ImageSource)GetValue(IconPressProperty); }
  185. set { SetValue(IconPressProperty, value); }
  186. }
  187. public double IconHeight
  188. {
  189. get { return (double)GetValue(IconHeightProperty); }
  190. set { SetValue(IconHeightProperty, value); }
  191. }
  192. public double IconWidth
  193. {
  194. get { return (double)GetValue(IconWidthProperty); }
  195. set { SetValue(IconWidthProperty, value); }
  196. }
  197. public Orientation IconContentOrientation
  198. {
  199. get { return (Orientation)GetValue(IconContentOrientationProperty); }
  200. set { SetValue(IconContentOrientationProperty, value); }
  201. }
  202. public Thickness IconContentMargin
  203. {
  204. get { return (Thickness)GetValue(IconContentMarginProperty); }
  205. set { SetValue(IconContentMarginProperty, value); }
  206. }
  207. #endregion
  208. }
  209. }