Loading.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. namespace CustomUI
  4. {
  5. public class Loading : Control
  6. {
  7. #region Private属性
  8. private FrameworkElement PART_Root;
  9. #endregion
  10. #region 依赖属性定义
  11. public bool IsActived
  12. {
  13. get { return (bool)GetValue(IsActivedProperty); }
  14. set { SetValue(IsActivedProperty, value); }
  15. }
  16. public static readonly DependencyProperty IsActivedProperty =
  17. DependencyProperty.Register("IsActived", typeof(bool), typeof(Loading), new PropertyMetadata(true, OnIsActivedChangedCallback));
  18. private static void OnIsActivedChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  19. {
  20. Loading loading = d as Loading;
  21. if(loading.PART_Root == null)
  22. {
  23. return;
  24. }
  25. VisualStateManager.GoToElementState(loading.PART_Root, (bool)e.NewValue ? "Active" : "Inactive", true);
  26. }
  27. public double SpeedRatio
  28. {
  29. get { return (double)GetValue(SpeedRatioProperty); }
  30. set { SetValue(SpeedRatioProperty, value); }
  31. }
  32. // Using a DependencyProperty as the backing store for SpeedRatio. This enables animation, styling, binding, etc...
  33. public static readonly DependencyProperty SpeedRatioProperty =
  34. DependencyProperty.Register("SpeedRatio", typeof(double), typeof(Loading), new PropertyMetadata(1d, OnSpeedRatioChangedCallback));
  35. private static void OnSpeedRatioChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  36. {
  37. Loading loading = d as Loading;
  38. if(loading.PART_Root == null || !loading.IsActived)
  39. {
  40. return;
  41. }
  42. loading.SetSpeedRatio(loading.PART_Root, loading.SpeedRatio);
  43. }
  44. public EnumLoadingType Type
  45. {
  46. get { return (EnumLoadingType)GetValue(TypeProperty); }
  47. set { SetValue(TypeProperty, value); }
  48. }
  49. // Using a DependencyProperty as the backing store for Type. This enables animation, styling, binding, etc...
  50. public static readonly DependencyProperty TypeProperty =
  51. DependencyProperty.Register("Type", typeof(EnumLoadingType), typeof(Loading), new PropertyMetadata(EnumLoadingType.DoubleArc));
  52. #endregion
  53. #region Constructors
  54. static Loading()
  55. {
  56. DefaultStyleKeyProperty.OverrideMetadata(typeof(Loading), new FrameworkPropertyMetadata(typeof(Loading)));
  57. }
  58. #endregion
  59. #region Override方法
  60. public override void OnApplyTemplate()
  61. {
  62. base.OnApplyTemplate();
  63. this.PART_Root = this.GetTemplateChild("PART_Root") as FrameworkElement;
  64. if(this.PART_Root != null)
  65. {
  66. VisualStateManager.GoToElementState(this.PART_Root, this.IsActived ? "Active" : "Inactive", true);
  67. this.SetSpeedRatio(this.PART_Root, this.SpeedRatio);
  68. }
  69. }
  70. #endregion
  71. #region Private方法
  72. private void SetSpeedRatio(FrameworkElement element, double speedRatio)
  73. {
  74. foreach (VisualStateGroup group in VisualStateManager.GetVisualStateGroups(element))
  75. {
  76. if (group.Name == "ActiveStates")
  77. {
  78. foreach (VisualState state in group.States)
  79. {
  80. if (state.Name == "Active")
  81. {
  82. state.Storyboard.SetSpeedRatio(element, speedRatio);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. #endregion
  89. }
  90. }