| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Collections.Generic;
- using Microsoft.Expression.Shapes;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- namespace CustomUI
- {
- /// <summary>
- /// 刻度盘控件
- /// </summary>
- [TemplatePart(Name = "PART_IncreaseCircle", Type = typeof(Arc))]
- public class Dashboard : Control
- {
- private Arc PART_IncreaseCircle;
- /// <summary>
- /// 保存角度变化前的角度值(用于动画)
- /// </summary>
- private double OldAngle;
- #region Constructors
- static Dashboard()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(Dashboard), new FrameworkPropertyMetadata(typeof(Dashboard)));
- }
- #endregion
- #region 依赖属性
- #region Angle 刻度盘起始角度
- /// <summary>
- /// 刻度盘起始角度依赖属性
- /// </summary>
- public static readonly DependencyProperty StartAngleProperty =
- DependencyProperty.Register(
- "StartAngle",
- typeof(double),
- typeof(Dashboard),
- new PropertyMetadata(0d));
- /// <summary>
- /// 刻度盘起始角度
- /// </summary>
- public double StartAngle
- {
- get { return (double)GetValue(StartAngleProperty); }
- set { SetValue(StartAngleProperty, value); }
- }
- #endregion
- #region Angle 刻度盘结束角度依赖属性
- /// <summary>
- /// 刻度盘结束角度依赖属性
- /// </summary>
- public static readonly DependencyProperty EndAngleProperty =
- DependencyProperty.Register(
- "EndAngle",
- typeof(double),
- typeof(Dashboard),
- new PropertyMetadata(0d));
- /// <summary>
- /// 刻度盘结束角度依赖属性
- /// </summary>
- public double EndAngle
- {
- get { return (double)GetValue(EndAngleProperty); }
- set { SetValue(EndAngleProperty, value); }
- }
- #endregion
- #region Minimum 最小值
- /// <summary>
- /// 最小值依赖属性,用于Binding
- /// </summary>
- public static readonly DependencyProperty MinimumProperty =
- DependencyProperty.Register(
- "Minimum",
- typeof(double),
- typeof(Dashboard),
- new PropertyMetadata(0.0));
- /// <summary>
- /// 获取或设置最小值.
- /// </summary>
- /// <value>最小值.</value>
- public double Minimum
- {
- get { return (double)GetValue(MinimumProperty); }
- set { SetValue(MinimumProperty, value); }
- }
- #endregion
- #region Maximum 最大值
- /// <summary>
- /// 最大值依赖属性,用于Binding
- /// </summary>
- public static readonly DependencyProperty MaximumProperty =
- DependencyProperty.Register(
- "Maximum",
- typeof(double),
- typeof(Dashboard),
- new PropertyMetadata(300.0));
- /// <summary>
- /// 获取或设置最大值.
- /// </summary>
- /// <value>最大值.</value>
- public double Maximum
- {
- get { return (double)GetValue(MaximumProperty); }
- set { SetValue(MaximumProperty, value); }
- }
- #endregion
- #region Value 当前值
- /// <summary>
- /// 最大值依赖属性,用于Binding
- /// </summary>
- public static readonly DependencyProperty ValueProperty =
- DependencyProperty.Register(
- "Value",
- typeof(double),
- typeof(Dashboard),
- new PropertyMetadata(0.0, new PropertyChangedCallback(OnValuePropertyChanged)));
- /// <summary>
- /// 获取或设置当前值
- /// </summary>
- public double Value
- {
- get { return (double)GetValue(ValueProperty); }
- set { SetValue(ValueProperty, value); }
- }
- private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- Dashboard dashboard = d as Dashboard;
- dashboard.OldAngle = dashboard.Angle;
- dashboard.SetAngle();
- dashboard.TransformAngle();
- }
- #endregion
- #region LongTickCount 长刻度个数
- public static readonly DependencyProperty LongTickCountProperty =
- DependencyProperty.Register(
- "LongTickCount",
- typeof(int),
- typeof(Dashboard),
- new PropertyMetadata(10));
- /// <summary>
- /// 获取或设置长刻度个数,用于设置刻度盘显示几个长刻度
- /// </summary>
- public int LongTickCount
- {
- get { return (int)GetValue(LongTickCountProperty); }
- set { SetValue(LongTickCountProperty, value); }
- }
- #endregion
- #region ShortTickCount 短刻度个数
- public static readonly DependencyProperty ShortTickCountProperty =
- DependencyProperty.Register(
- "ShortTickCount",
- typeof(int),
- typeof(Dashboard),
- new PropertyMetadata(3));
- /// <summary>
- /// 获取或设置两个长刻度之间的短刻度的个数
- /// </summary>
- public int ShortTickCount
- {
- get { return (int)GetValue(ShortTickCountProperty); }
- set { SetValue(ShortTickCountProperty, value); }
- }
- #endregion
- #region TickDurtion 刻度改变时的动画显示时长
- public static readonly DependencyProperty TickDurtionProperty = DependencyProperty.Register("TickDurtion"
- , typeof(Duration)
- , typeof(Dashboard),
- new PropertyMetadata(new Duration(TimeSpan.FromMilliseconds(400))));
- /// <summary>
- /// 刻度改变时的动画显示时长
- /// </summary>
- public Duration TickDurtion
- {
- get { return (Duration)GetValue(TickDurtionProperty); }
- set { SetValue(TickDurtionProperty, value); }
- }
- #endregion
- #region Skin 刻度盘样式
- public static readonly DependencyProperty SkinProperty = DependencyProperty.Register("Skin",
- typeof(DashboardSkinEnum),
- typeof(Dashboard),
- new PropertyMetadata(DashboardSkinEnum.Speed));
- /// <summary>
- /// 刻度盘样式
- /// </summary>
- public DashboardSkinEnum Skin
- {
- get { return (DashboardSkinEnum)GetValue(SkinProperty); }
- set { SetValue(SkinProperty, value); }
- }
- #endregion
- #region ShortTicksBrush 短刻度颜色
- public static readonly DependencyProperty ShortTicksBrushProperty = DependencyProperty.Register("ShortTicksBrush"
- , typeof(Brush)
- , typeof(Dashboard));
- /// <summary>
- /// 短刻度颜色
- /// </summary>
- public Brush ShortTicksBrush
- {
- get { return (Brush)GetValue(ShortTicksBrushProperty); }
- set { SetValue(ShortTicksBrushProperty, value); }
- }
- #endregion
- #region LongTicksBrush 长刻度颜色
- public static readonly DependencyProperty LongTicksBrushProperty = DependencyProperty.Register("LongTicksBrush"
- , typeof(Brush)
- , typeof(Dashboard));
- /// <summary>
- /// 长刻度颜色
- /// </summary>
- public Brush LongTicksBrush
- {
- get { return (Brush)GetValue(LongTicksBrushProperty); }
- set { SetValue(LongTicksBrushProperty, value); }
- }
- #endregion
- #region Content
- public object Content
- {
- get { return (object)GetValue(ContentProperty); }
- set { SetValue(ContentProperty, value); }
- }
- public static readonly DependencyProperty ContentProperty =
- DependencyProperty.Register("Content", typeof(object), typeof(Dashboard));
- #endregion
- #region ContentTemplate
- public DataTemplate ContentTemplate
- {
- get { return (DataTemplate)GetValue(ContentTemplateProperty); }
- set { SetValue(ContentTemplateProperty, value); }
- }
-
- public static readonly DependencyProperty ContentTemplateProperty =
- DependencyProperty.Register("ContentTemplate", typeof(DataTemplate), typeof(Dashboard));
- #endregion
- #endregion
- #region Private依赖属性
- #region Angle 刻度盘当前值所对应的角度
- /// <summary>
- /// 刻度盘当前值所对应的角度依赖属性
- /// </summary>
- public static readonly DependencyProperty AngleProperty =
- DependencyProperty.Register(
- "Angle",
- typeof(double),
- typeof(Dashboard),
- new PropertyMetadata(0d));
- /// <summary>
- /// 刻度盘当前值所对应的角度
- /// </summary>
- public double Angle
- {
- get { return (double)GetValue(AngleProperty); }
- private set { SetValue(AngleProperty, value); }
- }
- #endregion
- #region ShortTicks 短刻度线集合
- /// <summary>
- /// 短刻度线依赖属性,用于Binding
- /// </summary>
- public static readonly DependencyProperty ShortTicksProperty =
- DependencyProperty.Register(
- "ShortTicks",
- typeof(IList<object>),
- typeof(Dashboard),
- new PropertyMetadata(null));
- /// <summary>
- /// 获取或设置短刻度线,用于绑定PathListBox的ItemsSource
- /// </summary>
- /// <value>短刻度线.</value>
- public IList<object> ShortTicks
- {
- get { return (IList<object>)GetValue(ShortTicksProperty); }
- private set { SetValue(ShortTicksProperty, value); }
- }
- #endregion
- #region LongTicks 长刻度线集合
- /// <summary>
- /// 长刻度线依赖属性,用于Binding
- /// </summary>
- public static readonly DependencyProperty LongTicksProperty =
- DependencyProperty.Register(
- "LongTicks",
- typeof(IList<object>),
- typeof(Dashboard),
- new PropertyMetadata(null));
- /// <summary>
- /// 获取或设置长刻度线,用于绑定PathListBox的ItemsSource
- /// </summary>
- /// <value>长刻度线.</value>
- public IList<object> LongTicks
- {
- get { return (IList<object>)GetValue(LongTicksProperty); }
- private set { SetValue(LongTicksProperty, value); }
- }
- #endregion
- #region LongTicks 长刻度线上显示的数字
- /// <summary>
- /// 长刻度线依赖属性,用于Binding
- /// </summary>
- public static readonly DependencyProperty NumberListProperty =
- DependencyProperty.Register(
- "NumberList",
- typeof(IList<object>),
- typeof(Dashboard),
- new PropertyMetadata(null));
- /// <summary>
- /// 获取或设置长刻度线,用于绑定PathListBox的ItemsSource
- /// </summary>
- /// <value>长刻度线.</value>
- public IList<object> NumberList
- {
- get { return (IList<object>)GetValue(NumberListProperty); }
- private set { SetValue(NumberListProperty, value); }
- }
- #endregion
-
- #endregion
- #region 重载
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- this.PART_IncreaseCircle = GetTemplateChild("PART_IncreaseCircle") as Arc;
- this.SetTicks();
- this.SetAngle();
- this.TransformAngle();
- }
- #endregion
- #region Private方法
- /// <summary>
- /// 设置刻度线
- /// </summary>
- private void SetTicks()
- {
- List<object> numbers = new List<object>();
- List<object> shortticks = new List<object>();
- List<object> longticks = new List<object>();
- for (int i = 0; i < this.LongTickCount; i++)
- {
- numbers.Add(Math.Round(this.Minimum + (this.Maximum - this.Minimum) / (this.LongTickCount - 1) * i));
- longticks.Add(new object());
- }
- for (int i = 0; i < (this.LongTickCount - 1) * (this.ShortTickCount + 1) + 1; i++)
- {
- shortticks.Add(new object());
- }
- this.ShortTicks = shortticks;
- this.LongTicks = longticks;
- this.NumberList = numbers;
- }
- /// <summary>
- /// 根据当前值设置圆弧的EndAngle
- /// </summary>
- private void SetAngle()
- {
- if(this.Value < this.Minimum)
- {
- this.Angle = this.StartAngle;
- return;
- }
- if(this.Value > this.Maximum)
- {
- this.Angle = this.EndAngle;
- return;
- }
- var diff = this.Maximum - this.Minimum;
- var valueDiff = this.Value - this.Minimum;
- this.Angle = this.StartAngle + (this.EndAngle - this.StartAngle) / diff * valueDiff;
- }
- /// <summary>
- /// 角度值变化动画
- /// </summary>
- private void TransformAngle()
- {
- if (this.PART_IncreaseCircle != null)
- {
- DoubleAnimation doubleAnimation = new DoubleAnimation(this.OldAngle, this.Angle, this.TickDurtion);
- this.PART_IncreaseCircle.BeginAnimation(Arc.EndAngleProperty, doubleAnimation);
- }
- }
- #endregion
- }
- }
|