MuchinfoPagerNumberRadioButton.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. namespace Muchinfo.DataPager.Base
  7. {
  8. public class MuchinfoRoutedEventArgs : RoutedEventArgs
  9. {
  10. public MuchinfoRoutedEventArgs()
  11. {
  12. }
  13. public MuchinfoRoutedEventArgs(RoutedEvent routedEvent)
  14. : this(routedEvent, null)
  15. {
  16. }
  17. public MuchinfoRoutedEventArgs(RoutedEvent routedEvent, object source)
  18. : base(routedEvent, source)
  19. {
  20. }
  21. }
  22. public class MuchinfoPagerNumberRadioButton : RadioButton
  23. {
  24. public static readonly DependencyProperty CornerRadiusProperty;
  25. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  26. public static readonly DependencyProperty InnerCornerRadiusProperty;
  27. public static readonly DependencyProperty IsBackgroundVisibleProperty;
  28. public static readonly RoutedEvent ActivateEvent;
  29. public event EventHandler<MuchinfoRoutedEventArgs> Activate
  30. {
  31. add
  32. {
  33. base.AddHandler(MuchinfoPagerNumberRadioButton.ActivateEvent, value);
  34. }
  35. remove
  36. {
  37. base.RemoveHandler(MuchinfoPagerNumberRadioButton.ActivateEvent, value);
  38. }
  39. }
  40. public CornerRadius CornerRadius
  41. {
  42. get
  43. {
  44. return (CornerRadius)base.GetValue(MuchinfoPagerNumberRadioButton.CornerRadiusProperty);
  45. }
  46. set
  47. {
  48. base.SetValue(MuchinfoPagerNumberRadioButton.CornerRadiusProperty, value);
  49. }
  50. }
  51. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  52. public CornerRadius InnerCornerRadius
  53. {
  54. get
  55. {
  56. return (CornerRadius)base.GetValue(MuchinfoPagerNumberRadioButton.InnerCornerRadiusProperty);
  57. }
  58. set
  59. {
  60. base.SetValue(MuchinfoPagerNumberRadioButton.InnerCornerRadiusProperty, value);
  61. }
  62. }
  63. [Browsable(false)]
  64. public bool IsBackgroundVisible
  65. {
  66. get
  67. {
  68. return (bool)base.GetValue(MuchinfoPagerNumberRadioButton.IsBackgroundVisibleProperty);
  69. }
  70. set
  71. {
  72. base.SetValue(MuchinfoPagerNumberRadioButton.IsBackgroundVisibleProperty, value);
  73. }
  74. }
  75. static MuchinfoPagerNumberRadioButton()
  76. {
  77. MuchinfoPagerNumberRadioButton.CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MuchinfoPagerNumberRadioButton), new PropertyMetadata(new PropertyChangedCallback(MuchinfoPagerNumberRadioButton.OnCornerRadiusChanged)));
  78. MuchinfoPagerNumberRadioButton.InnerCornerRadiusProperty = DependencyProperty.Register("InnerCornerRadius", typeof(CornerRadius), typeof(MuchinfoPagerNumberRadioButton), new PropertyMetadata());
  79. MuchinfoPagerNumberRadioButton.IsBackgroundVisibleProperty = DependencyProperty.Register("IsBackgroundVisible", typeof(bool), typeof(MuchinfoPagerNumberRadioButton), new PropertyMetadata(true, new PropertyChangedCallback(MuchinfoPagerNumberRadioButton.OnIsBackgroundVisiblePropertyChanged)));
  80. MuchinfoPagerNumberRadioButton.ActivateEvent = EventManager.RegisterRoutedEvent("Activate", RoutingStrategy.Bubble, typeof(EventHandler<MuchinfoRoutedEventArgs>), typeof(MuchinfoPagerNumberRadioButton));
  81. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MuchinfoPagerNumberRadioButton), new FrameworkPropertyMetadata(typeof(MuchinfoPagerNumberRadioButton)));
  82. }
  83. public override void OnApplyTemplate()
  84. {
  85. base.OnApplyTemplate();
  86. this.UpdateVisualStates();
  87. this.UpdateBackgroundVisibility();
  88. }
  89. internal void UpdateVisualStates()
  90. {
  91. if (!base.IsEnabled)
  92. {
  93. if (base.IsChecked == true)
  94. {
  95. VisualStateManager.GoToState(this, "DisabledChecked", true);
  96. }
  97. else
  98. {
  99. VisualStateManager.GoToState(this, "Disabled", true);
  100. }
  101. }
  102. else
  103. {
  104. if (base.IsPressed)
  105. {
  106. VisualStateManager.GoToState(this, "Pressed", true);
  107. }
  108. else
  109. {
  110. if (base.IsMouseOver)
  111. {
  112. if (base.IsChecked == true)
  113. {
  114. VisualStateManager.GoToState(this, "MouseOverChecked", true);
  115. }
  116. else
  117. {
  118. VisualStateManager.GoToState(this, "MouseOver", true);
  119. }
  120. }
  121. else
  122. {
  123. VisualStateManager.GoToState(this, "Normal", true);
  124. }
  125. }
  126. }
  127. if (base.IsChecked == true)
  128. {
  129. VisualStateManager.GoToState(this, "Checked", true);
  130. }
  131. else
  132. {
  133. if (base.IsChecked == false)
  134. {
  135. VisualStateManager.GoToState(this, "Unchecked", true);
  136. }
  137. else
  138. {
  139. if (!VisualStateManager.GoToState(this, "Indeterminate", true))
  140. {
  141. VisualStateManager.GoToState(this, "Unchecked", true);
  142. }
  143. }
  144. }
  145. if (base.IsFocused && base.IsEnabled)
  146. {
  147. VisualStateManager.GoToState(this, "Focused", true);
  148. }
  149. else
  150. {
  151. VisualStateManager.GoToState(this, "Unfocused", true);
  152. }
  153. if (this.IsBackgroundVisible || base.IsMouseOver || base.IsPressed)
  154. {
  155. VisualStateManager.GoToState(this, "BackgroundVisible", true);
  156. }
  157. else
  158. {
  159. VisualStateManager.GoToState(this, "BackgroundHidden", true);
  160. }
  161. if (this.IsBackgroundVisible)
  162. {
  163. VisualStateManager.GoToState(this, "BackgroundIsVisible", false);
  164. return;
  165. }
  166. VisualStateManager.GoToState(this, "BackgroundIsHidden", false);
  167. }
  168. internal void UpdateBackgroundVisibility()
  169. {
  170. this.UpdateVisualStates();
  171. }
  172. internal void OnToggleInternal()
  173. {
  174. this.OnToggle();
  175. }
  176. protected internal virtual void OnActivate()
  177. {
  178. base.RaiseEvent(new MuchinfoRoutedEventArgs(MuchinfoPagerNumberRadioButton.ActivateEvent, this));
  179. }
  180. protected override void OnToggle()
  181. {
  182. base.OnToggle();
  183. this.OnActivate();
  184. //if (base.IsChecked == true)
  185. //{
  186. // TraceMonitor.TrackAtomicFeature(this, "Checked");
  187. // return;
  188. //}
  189. //if (base.IsChecked == false)
  190. //{
  191. // TraceMonitor.TrackAtomicFeature(this, "Unchecked");
  192. //}
  193. }
  194. protected override void OnMouseEnter(MouseEventArgs e)
  195. {
  196. base.OnMouseEnter(e);
  197. this.UpdateVisualStates();
  198. this.UpdateBackgroundVisibility();
  199. }
  200. protected override void OnMouseLeave(MouseEventArgs e)
  201. {
  202. base.OnMouseLeave(e);
  203. this.UpdateVisualStates();
  204. this.UpdateBackgroundVisibility();
  205. }
  206. protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  207. {
  208. base.OnMouseLeftButtonUp(e);
  209. this.UpdateVisualStates();
  210. this.UpdateBackgroundVisibility();
  211. }
  212. protected override void OnLostFocus(RoutedEventArgs e)
  213. {
  214. base.OnLostFocus(e);
  215. this.UpdateBackgroundVisibility();
  216. }
  217. protected override void OnGotFocus(RoutedEventArgs e)
  218. {
  219. base.OnGotFocus(e);
  220. this.UpdateBackgroundVisibility();
  221. }
  222. private static void OnCornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  223. {
  224. MuchinfoPagerNumberRadioButton radRadioButton = d as MuchinfoPagerNumberRadioButton;
  225. CornerRadius cornerRadius = (CornerRadius)e.NewValue;
  226. if (radRadioButton != null)
  227. {
  228. radRadioButton.InnerCornerRadius = new CornerRadius(Math.Max(0.0, cornerRadius.TopLeft - 1.0), Math.Max(0.0, cornerRadius.TopRight - 1.0), Math.Max(0.0, cornerRadius.BottomRight - 1.0), Math.Max(0.0, cornerRadius.BottomLeft - 1.0));
  229. }
  230. }
  231. private static void OnIsBackgroundVisiblePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  232. {
  233. MuchinfoPagerNumberRadioButton radRadioButton = d as MuchinfoPagerNumberRadioButton;
  234. }
  235. }
  236. }