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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Security.Cryptography;
 
namespace XHandler.Class
{
    /// <summary>
    /// 工具类
    /// </summary>
    public class Utilities
    {
        /// <summary>
        /// 获取父元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T FindVisualParent<T>(DependencyObject obj) where T : class
        {
            while (obj != null)
            {
                if (obj is T)
                {
                    return obj as T;
                }
 
                obj = VisualTreeHelper.GetParent(obj);
            }
 
            return null;
        }
 
        /// <summary>
        /// 获取第一个子控件
        /// </summary>
        /// <typeparam name="ChildType"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is ChildType)
                {
                    return child as ChildType;
                }
                else
                {
                    ChildType childOfChildren = FindVisualChild<ChildType>(child);
                    if (childOfChildren != null)
                    {
                        return childOfChildren;
                    }
                }
            }
            return null;
        }
 
        /// <summary>
        /// 获取指定类型的所有子控件
        /// </summary>
        /// <typeparam name="ChildType"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<ChildType> FindAllVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
        {
            List<ChildType > children = new List < ChildType >();
 
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is ChildType)
                {
                    children.Add(child as ChildType);
                }
                else
                {
                    List<ChildType> childOfChildren = FindAllVisualChild<ChildType>(child);
                    if (childOfChildren != null&& childOfChildren.Count>0)
                    {
                        children.AddRange(childOfChildren);
                    }
                }
            }
            return children;
        }
 
        /// <summary>
        /// 获取最后一个子控件
        /// </summary>
        /// <typeparam name="ChildType"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static ChildType FindLastVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
        {
            for (int i = VisualTreeHelper.GetChildrenCount(obj)-1; i >=0; i--)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is ChildType)
                {
                    return child as ChildType;
                }
                else
                {
                    ChildType childOfChildren = FindLastVisualChild<ChildType>(child);
                    if (childOfChildren != null)
                    {
                        return childOfChildren;
                    }
                }
            }
            return null;
        }
 
        /// <summary>
        /// 移除元素
        /// </summary>
        /// <param name="element"></param>
        public static void RemoveChild(UIElement element)
        {
            Window wnd = Application.Current.MainWindow;
            Grid grid = Utilities.FindVisualChild<Grid>(wnd);
            grid.Children.Remove(element);
        }
 
 
        public static TreeViewItem GetDataFromTreeViewItem(TreeView source, Point point)
        {
            UIElement element = source.InputHitTest(point) as UIElement; //直接获取最底层的元素,这里是textblock
            if (element != null)
            {
                return FindVisualParent<TreeViewItem>(element);//获取父控件
            }
            return null;
        }
 
        public static TreeViewItem FindTreeViewItem(ItemsControl item, object data)
        {
            TreeViewItem findItem = null;
            bool itemIsExpand = false;
            if (item is TreeViewItem)
            {
                TreeViewItem tviCurrent = item as TreeViewItem;
                itemIsExpand = tviCurrent.IsExpanded;
                if (!tviCurrent.IsExpanded)
                {
                    //如果这个TreeViewItem未展开过,则不能通过ItemContainerGenerator来获得TreeViewItem
                    tviCurrent.SetValue(TreeViewItem.IsExpandedProperty, true);
                    //必须使用UpdaeLayour才能获取到TreeViewItem
                    tviCurrent.UpdateLayout();
                }
            }
            for (int i = 0; i < item.Items.Count; i++)
            {
                TreeViewItem tvItem = (TreeViewItem)item.ItemContainerGenerator.ContainerFromIndex(i);
                if (tvItem == null)
                    continue;
                object itemData = item.Items[i];
                if (itemData == data)
                {
                    findItem = tvItem;
                    break;
                }
                else if (tvItem.Items.Count > 0)
                {
                    findItem = FindTreeViewItem(tvItem, data);
                    if (findItem != null)
                        break;
                }
            }
            if (findItem == null)
            {
                TreeViewItem tviCurrent = item as TreeViewItem;
                tviCurrent.SetValue(TreeViewItem.IsExpandedProperty, itemIsExpand);
                tviCurrent.UpdateLayout();
            }
 
            return findItem;
        }
 
        public static bool isNumberic(string _string)
        {
            if (string.IsNullOrEmpty(_string))
                return false;
            foreach (char c in _string)
            {
                if (!char.IsDigit(c))
                    //if(c<'0' || c>'9')//最好的方法,在下面测试数据中再加一个0,然后这种方法效率会搞10毫秒左右
                    return false;
            }
            return true;
        }
 
        public static string GetMD5(string password)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bytValue, bytHash;
            bytValue = System.Text.Encoding.UTF8.GetBytes(password);
            bytHash = md5.ComputeHash(bytValue);
            md5.Clear();
            string sTemp = "";
            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
            }
            return sTemp.ToLower();
        }
 
        /// <summary>
        /// 查询通道在通道组中的下标值
        /// </summary>
        /// <param name="chArray"></param>
        /// <param name="chNum"></param>
        /// <returns></returns>
        public static int GetArrayIndex(int[] chArray,int chNum)
        {
            int index = 0;
            for(int i=0;i< chArray.Length;i++)
            {
                if (chArray[i]==chNum)
                {
                    index = i; break;
                }
            }
            return index;
        }
 
        public static float[] GetFloatArrayFromStringArray(string[] strArray)
        {
            float[] floatArray= new float[strArray.Length];
            for(int i=0;i<strArray.Length;i++)
            {
                floatArray[i] = (float)Convert.ToDouble(strArray[i]);
            }
            return floatArray;
        }
    }
}