123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 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
- {
- /// <summary>
- /// BulletCheckBox.xaml 的交互逻辑
- /// </summary>
- [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;
- }
- /// <summary>
- /// 默认文本(未选中)
- /// </summary>
- 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-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();
- }
- 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/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();
- }
- 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"));
- /// <summary>
- /// 选中状态文本
- /// </summary>
- 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));
- /// <summary>
- /// 选中状态前景样式
- /// </summary>
- 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));
- /// <summary>
- /// 选中状态背景色
- /// </summary>
- public Brush CheckedBackground
- {
- get { return (Brush)GetValue(CheckedBackgroundProperty); }
- set { SetValue(CheckedBackgroundProperty, value); }
- }
- static BulletCheckBox()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(BulletCheckBox), new FrameworkPropertyMetadata(typeof(BulletCheckBox)));
- }
- }
- }
|