using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Media; using System.Windows; namespace PDF_Master.CustomControl { public class StepBarItem : ContentControl { #region 依赖属性定义 public string Number { get { return (string)GetValue(NumberProperty); } set { SetValue(NumberProperty, value); } } public static readonly DependencyProperty NumberProperty = DependencyProperty.Register("Number", typeof(string), typeof(StepBarItem)); #endregion 依赖属性定义 #region Constructors static StepBarItem() { DefaultStyleKeyProperty.OverrideMetadata(typeof(StepBarItem), new FrameworkPropertyMetadata(typeof(StepBarItem))); } #endregion Constructors } public class StepBar : ItemsControl { #region Progress public int CurrentIndex { get { return (int)GetValue(CurrentIndexProperty); } set { SetValue(CurrentIndexProperty, value); } } public static readonly DependencyProperty CurrentIndexProperty = DependencyProperty.Register("CurrentIndex", typeof(int), typeof(StepBar), new PropertyMetadata(0)); public Brush ActivateColor { get { return (Brush)GetValue(ActivateColorProperty); } set { SetValue(ActivateColorProperty, value); } } // Using a DependencyProperty as the backing store for ActivateColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty ActivateColorProperty = DependencyProperty.Register("ActivateColor", typeof(Brush), typeof(StepBar), new PropertyMetadata(new SolidColorBrush(Colors.Green))); public Brush UnActivateColor { get { return (Brush)GetValue(UnActivateColorProperty); } set { SetValue(UnActivateColorProperty, value); } } // Using a DependencyProperty as the backing store for UnActivateColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty UnActivateColorProperty = DependencyProperty.Register("UnActivateColor", typeof(Brush), typeof(StepBar), new PropertyMetadata(new SolidColorBrush(Colors.Gray))); /// /// 线的最小长度 /// public double LineMinLength { get { return (double)GetValue(LineMinLengthProperty); } set { SetValue(LineMinLengthProperty, value); } } // Using a DependencyProperty as the backing store for LineMinLength. This enables animation, styling, binding, etc... public static readonly DependencyProperty LineMinLengthProperty = DependencyProperty.Register("LineMinLength", typeof(double), typeof(StepBar), new PropertyMetadata(50d)); public Orientation Orientation { get { return (Orientation)GetValue(OrientationProperty); } set { SetValue(OrientationProperty, value); } } // Using a DependencyProperty as the backing store for Orientation. This enables animation, styling, binding, etc... public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(StepBar), new PropertyMetadata(Orientation.Horizontal)); #endregion Progress #region Constructors static StepBar() { DefaultStyleKeyProperty.OverrideMetadata(typeof(StepBar), new FrameworkPropertyMetadata(typeof(StepBar))); } #endregion Constructors #region Override方法 protected override DependencyObject GetContainerForItemOverride() { return new StepBarItem(); } protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { //设置Item的显示数字 StepBarItem stepBarItem = element as StepBarItem; ItemsControl itemsControl = ItemsControl.ItemsControlFromItemContainer(stepBarItem); int index = itemsControl.ItemContainerGenerator.IndexFromContainer(stepBarItem); stepBarItem.Number = Convert.ToString(++index); base.PrepareContainerForItemOverride(element, item); } protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) { base.OnItemsChanged(e); //ItemsControl数量变化时,重新设置各个Item的显示的数字 for (int i = 0; i < this.Items.Count; i++) { StepBarItem stepBarItem = this.ItemContainerGenerator.ContainerFromIndex(i) as StepBarItem; if (stepBarItem != null) { int temp = i; stepBarItem.Number = Convert.ToString(++temp); } } //进度重新回到第一个 //this.Progress = 0; } #endregion Override方法 } }