schangxiang@126.com
2025-11-04 ca398287db3f5ea01f03aac4a85edb13b28100a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace XHandler.Controls
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfAppStepBar"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfAppStepBar;assembly=WpfAppStepBar"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目,然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:StepBar/>
    ///
    /// </summary>
    public class StepBar : ItemsControl
    {
        #region Private属性
 
        #endregion
 
        #region 依赖属性定义
        public int StepIndex
        {
            get { return (int)GetValue(StepIndexProperty); }
            set { SetValue(StepIndexProperty, value); }
        }
 
        public static readonly DependencyProperty StepIndexProperty =
            DependencyProperty.Register("StepIndex", typeof(int), typeof(StepBar), new PropertyMetadata(0, OnStepIndexChanged));
 
        private static void OnProgressCoerceValueCallback(DependencyObject d, object baseValue)
        {
            //不让Progress超出边界
            StepBar stepBar = d as StepBar;
            int newValue = Convert.ToInt32(baseValue);
            //if (newValue < 0)
            //{
            //    return 0;
            //}
            //else if (newValue >= stepBar.Items.Count)
            //{
            //    return stepBar.Items.Count - 1;
            //}
            //return newValue;
        }
 
        private static void OnStepIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            StepBar step = (StepBar)d;
            int stepIndex = (int)e.NewValue;
            step.UpdateStepItemState(stepIndex);
        }
        private void UpdateStepItemState(int stepIndex)
        {
            int count = Items.Count;
            if (count <= 0)
            {
                return;
            }
 
            for (int i = 0; i < Items.Count; i++)
            {
                if (ItemContainerGenerator.ContainerFromIndex(i) is StepBarItem stepItem)
                {
                    stepItem.State = i < stepIndex ? StepState.Complete : i == stepIndex ? StepState.Busy : StepState.Default;
                    int a = 0;
                }
            }
        }
 
        #endregion
 
        #region 依赖属性set get
 
        #endregion
 
        #region Constructors
        static StepBar()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(StepBar), new FrameworkPropertyMetadata(typeof(StepBar)));
        }
        #endregion
 
        #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);
 
            UpdateStepItemState(this.StepIndex);
        }
 
        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);
                }
            }
            //进度重新回到第一个
            //int a=this.StepIndex ;
        }
        #endregion
 
        #region Private方法
 
        #endregion
 
        public void Next()
        {
            if (StepIndex >= Items.Count)
            {
                StepIndex = Items.Count - 1;
            }
            StepIndex++;
        }
 
 
        public void Prev()
        {
            if (StepIndex < 0)
            {
                StepIndex = -1;
            }
            else
            {
                StepIndex--;
            }
        }
    }
}