using System; using System.Collections.Generic; using System.Text; using System.Windows.Controls; using System.Windows; using System.Windows.Media; using System.Runtime.InteropServices; using System.Windows.Media.Animation; namespace HandyControl.Controls { /// /// BulletCheckBox.xaml 的交互逻辑 /// [TemplatePart(Name =ROOT_NAME, Type = typeof(Border))] [TemplatePart(Name = STATUS_BALL_NAME, Type = typeof(Border))] [TemplatePart(Name = TEXT_NAME, Type = typeof(TextBlock))] public class BulletCheckBox : CheckBox { private Border rootborder; private Border statusborder; private TextBlock textblock; private const string ROOT_NAME = "PART_Root"; private const string STATUS_BALL_NAME = "PART_StatusBall"; private const string TEXT_NAME = "PART_TXT"; public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(BulletCheckBox), new PropertyMetadata("Off")); public override void OnApplyTemplate() { base.OnApplyTemplate(); rootborder = GetTemplateChild(ROOT_NAME) as Border; statusborder = GetTemplateChild(STATUS_BALL_NAME) as Border; textblock = GetTemplateChild(TEXT_NAME) as TextBlock; } /// /// 默认文本(未选中) /// public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) { base.OnRenderSizeChanged(sizeInfo); if(IsChecked.HasValue && IsChecked.Value) { StartCheckedAnimation(); } else { StartUncheckedAnimation(); } } protected override void OnChecked(RoutedEventArgs e) { base.OnChecked(e); StartCheckedAnimation(); } private void StartCheckedAnimation() { if (statusborder == null || textblock == null) return; Storyboard statstoryboard = new Storyboard(); DoubleAnimation stat = new DoubleAnimation((statusborder.RenderTransform as TranslateTransform).X, ActualWidth - ActualHeight, new Duration(TimeSpan.FromSeconds(0.2))) { AutoReverse = false, }; Storyboard.SetTarget(stat, statusborder); Storyboard.SetTargetProperty(stat, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)")); statstoryboard.Children.Add(stat); DoubleAnimation txt = new DoubleAnimation((textblock.RenderTransform as TranslateTransform).X, (ActualWidth-ActualHeight)/2- ActualHeight*1.5+textblock.ActualWidth/2, new Duration(TimeSpan.FromSeconds(0.2))) { AutoReverse = false, }; Storyboard.SetTarget(txt, textblock); Storyboard.SetTargetProperty(txt, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)")); statstoryboard.Children.Add(txt); statstoryboard.Begin(); } private void StartUncheckedAnimation() { if (statusborder == null || textblock == null) return; Storyboard statstoryboard = new Storyboard(); DoubleAnimation stat = new DoubleAnimation((statusborder.RenderTransform as TranslateTransform).X, 0, new Duration(TimeSpan.FromSeconds(0.2))) { AutoReverse = false, }; Storyboard.SetTarget(stat, statusborder); Storyboard.SetTargetProperty(stat, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)")); statstoryboard.Children.Add(stat); DoubleAnimation txt = new DoubleAnimation((textblock.RenderTransform as TranslateTransform).X, (ActualWidth - ActualHeight) / 2 - textblock.ActualWidth, new Duration(TimeSpan.FromSeconds(0.2))) { AutoReverse = false, }; Storyboard.SetTarget(txt, textblock); Storyboard.SetTargetProperty(txt, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)")); statstoryboard.Children.Add(txt); statstoryboard.Begin(); } protected override void OnUnchecked(RoutedEventArgs e) { base.OnUnchecked(e); StartUncheckedAnimation(); } public static readonly DependencyProperty CheckedTextProperty = DependencyProperty.Register( "CheckedText", typeof(string), typeof(BulletCheckBox), new PropertyMetadata("On")); /// /// 选中状态文本 /// public string CheckedText { get { return (string)GetValue(CheckedTextProperty); } set { SetValue(CheckedTextProperty, value); } } public static readonly DependencyProperty CheckedForegroundProperty = DependencyProperty.Register("CheckedForeground", typeof(Brush), typeof(BulletCheckBox), new PropertyMetadata(Brushes.WhiteSmoke)); /// /// 选中状态前景样式 /// public Brush CheckedForeground { get { return (Brush)GetValue(CheckedForegroundProperty); } set { SetValue(CheckedForegroundProperty, value); } } public static readonly DependencyProperty CheckedBackgroundProperty = DependencyProperty.Register("CheckedBackground", typeof(Brush), typeof(BulletCheckBox), new PropertyMetadata(Brushes.LimeGreen)); /// /// 选中状态背景色 /// public Brush CheckedBackground { get { return (Brush)GetValue(CheckedBackgroundProperty); } set { SetValue(CheckedBackgroundProperty, value); } } static BulletCheckBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(BulletCheckBox), new FrameworkPropertyMetadata(typeof(BulletCheckBox))); } } }