BulletCheckBox.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Controls;
  5. using System.Windows;
  6. using System.Windows.Media;
  7. using System.Runtime.InteropServices;
  8. using System.Windows.Media.Animation;
  9. namespace HandyControl.Controls
  10. {
  11. /// <summary>
  12. /// BulletCheckBox.xaml 的交互逻辑
  13. /// </summary>
  14. [TemplatePart(Name =ROOT_NAME, Type = typeof(Border))]
  15. [TemplatePart(Name = STATUS_BALL_NAME, Type = typeof(Border))]
  16. [TemplatePart(Name = TEXT_NAME, Type = typeof(TextBlock))]
  17. public class BulletCheckBox : CheckBox
  18. {
  19. private Border rootborder;
  20. private Border statusborder;
  21. private TextBlock textblock;
  22. private const string ROOT_NAME = "PART_Root";
  23. private const string STATUS_BALL_NAME = "PART_StatusBall";
  24. private const string TEXT_NAME = "PART_TXT";
  25. public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
  26. "Text", typeof(string), typeof(BulletCheckBox), new PropertyMetadata("Off"));
  27. public override void OnApplyTemplate()
  28. {
  29. base.OnApplyTemplate();
  30. rootborder = GetTemplateChild(ROOT_NAME) as Border;
  31. statusborder = GetTemplateChild(STATUS_BALL_NAME) as Border;
  32. textblock = GetTemplateChild(TEXT_NAME) as TextBlock;
  33. }
  34. /// <summary>
  35. /// 默认文本(未选中)
  36. /// </summary>
  37. public string Text
  38. {
  39. get { return (string)GetValue(TextProperty); }
  40. set { SetValue(TextProperty, value); }
  41. }
  42. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  43. {
  44. base.OnRenderSizeChanged(sizeInfo);
  45. if(IsChecked.HasValue && IsChecked.Value)
  46. {
  47. StartCheckedAnimation();
  48. }
  49. else
  50. {
  51. StartUncheckedAnimation();
  52. }
  53. }
  54. protected override void OnChecked(RoutedEventArgs e)
  55. {
  56. base.OnChecked(e);
  57. StartCheckedAnimation();
  58. }
  59. private void StartCheckedAnimation()
  60. {
  61. if (statusborder == null || textblock == null) return;
  62. Storyboard statstoryboard = new Storyboard();
  63. DoubleAnimation stat = new DoubleAnimation((statusborder.RenderTransform as TranslateTransform).X, ActualWidth - ActualHeight, new Duration(TimeSpan.FromSeconds(0.2)))
  64. {
  65. AutoReverse = false,
  66. };
  67. Storyboard.SetTarget(stat, statusborder);
  68. Storyboard.SetTargetProperty(stat, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
  69. statstoryboard.Children.Add(stat);
  70. DoubleAnimation txt = new DoubleAnimation((textblock.RenderTransform as TranslateTransform).X, (ActualWidth-ActualHeight)/2- ActualHeight*1.5+textblock.ActualWidth/2, new Duration(TimeSpan.FromSeconds(0.2)))
  71. {
  72. AutoReverse = false,
  73. };
  74. Storyboard.SetTarget(txt, textblock);
  75. Storyboard.SetTargetProperty(txt, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
  76. statstoryboard.Children.Add(txt);
  77. statstoryboard.Begin();
  78. }
  79. private void StartUncheckedAnimation()
  80. {
  81. if (statusborder == null || textblock == null) return;
  82. Storyboard statstoryboard = new Storyboard();
  83. DoubleAnimation stat = new DoubleAnimation((statusborder.RenderTransform as TranslateTransform).X, 0, new Duration(TimeSpan.FromSeconds(0.2)))
  84. {
  85. AutoReverse = false,
  86. };
  87. Storyboard.SetTarget(stat, statusborder);
  88. Storyboard.SetTargetProperty(stat, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
  89. statstoryboard.Children.Add(stat);
  90. DoubleAnimation txt = new DoubleAnimation((textblock.RenderTransform as TranslateTransform).X, (ActualWidth - ActualHeight) / 2 - textblock.ActualWidth, new Duration(TimeSpan.FromSeconds(0.2)))
  91. {
  92. AutoReverse = false,
  93. };
  94. Storyboard.SetTarget(txt, textblock);
  95. Storyboard.SetTargetProperty(txt, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
  96. statstoryboard.Children.Add(txt);
  97. statstoryboard.Begin();
  98. }
  99. protected override void OnUnchecked(RoutedEventArgs e)
  100. {
  101. base.OnUnchecked(e);
  102. StartUncheckedAnimation();
  103. }
  104. public static readonly DependencyProperty CheckedTextProperty = DependencyProperty.Register(
  105. "CheckedText", typeof(string), typeof(BulletCheckBox), new PropertyMetadata("On"));
  106. /// <summary>
  107. /// 选中状态文本
  108. /// </summary>
  109. public string CheckedText
  110. {
  111. get { return (string)GetValue(CheckedTextProperty); }
  112. set { SetValue(CheckedTextProperty, value); }
  113. }
  114. public static readonly DependencyProperty CheckedForegroundProperty =
  115. DependencyProperty.Register("CheckedForeground", typeof(Brush), typeof(BulletCheckBox), new PropertyMetadata(Brushes.WhiteSmoke));
  116. /// <summary>
  117. /// 选中状态前景样式
  118. /// </summary>
  119. public Brush CheckedForeground
  120. {
  121. get { return (Brush)GetValue(CheckedForegroundProperty); }
  122. set { SetValue(CheckedForegroundProperty, value); }
  123. }
  124. public static readonly DependencyProperty CheckedBackgroundProperty =
  125. DependencyProperty.Register("CheckedBackground", typeof(Brush), typeof(BulletCheckBox), new PropertyMetadata(Brushes.LimeGreen));
  126. /// <summary>
  127. /// 选中状态背景色
  128. /// </summary>
  129. public Brush CheckedBackground
  130. {
  131. get { return (Brush)GetValue(CheckedBackgroundProperty); }
  132. set { SetValue(CheckedBackgroundProperty, value); }
  133. }
  134. static BulletCheckBox()
  135. {
  136. DefaultStyleKeyProperty.OverrideMetadata(typeof(BulletCheckBox), new FrameworkPropertyMetadata(typeof(BulletCheckBox)));
  137. }
  138. }
  139. }