StepBar.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows;
  10. namespace PDF_Master.CustomControl
  11. {
  12. public class StepBarItem : ContentControl
  13. {
  14. #region 依赖属性定义
  15. public string Number
  16. {
  17. get { return (string)GetValue(NumberProperty); }
  18. set { SetValue(NumberProperty, value); }
  19. }
  20. public static readonly DependencyProperty NumberProperty =
  21. DependencyProperty.Register("Number", typeof(string), typeof(StepBarItem));
  22. #endregion 依赖属性定义
  23. #region Constructors
  24. static StepBarItem()
  25. {
  26. DefaultStyleKeyProperty.OverrideMetadata(typeof(StepBarItem), new FrameworkPropertyMetadata(typeof(StepBarItem)));
  27. }
  28. #endregion Constructors
  29. }
  30. public class StepBar : ItemsControl
  31. {
  32. #region Progress
  33. public int CurrentIndex
  34. {
  35. get { return (int)GetValue(CurrentIndexProperty); }
  36. set { SetValue(CurrentIndexProperty, value); }
  37. }
  38. public static readonly DependencyProperty CurrentIndexProperty =
  39. DependencyProperty.Register("CurrentIndex", typeof(int), typeof(StepBar), new PropertyMetadata(0));
  40. public Brush ActivateColor
  41. {
  42. get { return (Brush)GetValue(ActivateColorProperty); }
  43. set { SetValue(ActivateColorProperty, value); }
  44. }
  45. // Using a DependencyProperty as the backing store for ActivateColor. This enables animation, styling, binding, etc...
  46. public static readonly DependencyProperty ActivateColorProperty =
  47. DependencyProperty.Register("ActivateColor", typeof(Brush), typeof(StepBar), new PropertyMetadata(new SolidColorBrush(Colors.Green)));
  48. public Brush UnActivateColor
  49. {
  50. get { return (Brush)GetValue(UnActivateColorProperty); }
  51. set { SetValue(UnActivateColorProperty, value); }
  52. }
  53. // Using a DependencyProperty as the backing store for UnActivateColor. This enables animation, styling, binding, etc...
  54. public static readonly DependencyProperty UnActivateColorProperty =
  55. DependencyProperty.Register("UnActivateColor", typeof(Brush), typeof(StepBar), new PropertyMetadata(new SolidColorBrush(Colors.Gray)));
  56. /// <summary>
  57. /// 线的最小长度
  58. /// </summary>
  59. public double LineMinLength
  60. {
  61. get { return (double)GetValue(LineMinLengthProperty); }
  62. set { SetValue(LineMinLengthProperty, value); }
  63. }
  64. // Using a DependencyProperty as the backing store for LineMinLength. This enables animation, styling, binding, etc...
  65. public static readonly DependencyProperty LineMinLengthProperty =
  66. DependencyProperty.Register("LineMinLength", typeof(double), typeof(StepBar), new PropertyMetadata(50d));
  67. public Orientation Orientation
  68. {
  69. get { return (Orientation)GetValue(OrientationProperty); }
  70. set { SetValue(OrientationProperty, value); }
  71. }
  72. // Using a DependencyProperty as the backing store for Orientation. This enables animation, styling, binding, etc...
  73. public static readonly DependencyProperty OrientationProperty =
  74. DependencyProperty.Register("Orientation", typeof(Orientation), typeof(StepBar), new PropertyMetadata(Orientation.Horizontal));
  75. #endregion Progress
  76. #region Constructors
  77. static StepBar()
  78. {
  79. DefaultStyleKeyProperty.OverrideMetadata(typeof(StepBar), new FrameworkPropertyMetadata(typeof(StepBar)));
  80. }
  81. #endregion Constructors
  82. #region Override方法
  83. protected override DependencyObject GetContainerForItemOverride()
  84. {
  85. return new StepBarItem();
  86. }
  87. protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
  88. {
  89. //设置Item的显示数字
  90. StepBarItem stepBarItem = element as StepBarItem;
  91. ItemsControl itemsControl = ItemsControl.ItemsControlFromItemContainer(stepBarItem);
  92. int index = itemsControl.ItemContainerGenerator.IndexFromContainer(stepBarItem);
  93. stepBarItem.Number = Convert.ToString(++index);
  94. base.PrepareContainerForItemOverride(element, item);
  95. }
  96. protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
  97. {
  98. base.OnItemsChanged(e);
  99. //ItemsControl数量变化时,重新设置各个Item的显示的数字
  100. for (int i = 0; i < this.Items.Count; i++)
  101. {
  102. StepBarItem stepBarItem = this.ItemContainerGenerator.ContainerFromIndex(i) as StepBarItem;
  103. if (stepBarItem != null)
  104. {
  105. int temp = i;
  106. stepBarItem.Number = Convert.ToString(++temp);
  107. }
  108. }
  109. //进度重新回到第一个
  110. //this.Progress = 0;
  111. }
  112. #endregion Override方法
  113. }
  114. }