ImageRadioButton .cs 9.0 KB

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