-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualHelperTreeExtension.cs
199 lines (186 loc) · 6.91 KB
/
VisualHelperTreeExtension.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
/// <summary>
/// WPF/Silverlight 查找控件扩展方法
/// </summary>
public static class VisualHelperTreeExtension
{
/// <summary>
/// 根据控件名称,查找父控件
/// elementName为空时,查找指定类型的父控件
/// </summary>
public static T GetParentByName<T>(this DependencyObject obj, string elementName)
where T : FrameworkElement
{
DependencyObject parent = VisualTreeHelper.GetParent(obj);
while (parent != null)
{
if ((parent is T) && (((T)parent).Name == elementName || string.IsNullOrEmpty(elementName)))
{
return (T)parent;
}
parent = VisualTreeHelper.GetParent(parent);
}
return null;
}
/// <summary>
/// 根据控件名称,查找子控件
/// elementName为空时,查找指定类型的子控件
/// </summary>
public static T GetChildByName<T>(this DependencyObject obj, string elementName)
where T : FrameworkElement
{
DependencyObject child = null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Name == elementName) || (string.IsNullOrEmpty(elementName)))
{
return (T)child;
}
else
{
T grandChild = GetChildByName<T>(child, elementName);
if (grandChild != null)
{
return grandChild;
}
}
}
return null;
}
/// <summary>
/// 根据控件名称,查找子控件集合
/// elementName为空时,查找指定类型的所有子控件
/// </summary>
public static List<T> GetChildsByName<T>(this DependencyObject obj, string elementName)
where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Name == elementName) || (string.IsNullOrEmpty(elementName)))
{
childList.Add((T)child);
}
else
{
List<T> grandChildList = GetChildsByName<T>(child, elementName);
if (grandChildList != null)
{
childList.AddRange(grandChildList);
}
}
}
return childList;
}
/// <summary>
/// 根据控件名称,查找子控件集合
/// elementName为空时,查找指定类型的所有子控件
/// </summary>
public static List<T> GetChildsByBlueName<T>(this DependencyObject obj, string elementName)
where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Name.Contains(elementName)) || (string.IsNullOrEmpty(elementName)))
{
childList.Add((T)child);
}
else
{
List<T> grandChildList = GetChildsByBlueName<T>(child, elementName);
if (grandChildList != null)
{
childList.AddRange(grandChildList);
}
}
}
return childList;
}
/// <summary>
/// 根据控件名称,查找父控件
/// elementUid为空时,查找指定类型的父控件
/// </summary>
public static T GetParentByUid<T>(this DependencyObject obj, string elementUid)
where T : FrameworkElement
{
DependencyObject parent = VisualTreeHelper.GetParent(obj);
while (parent != null)
{
if ((parent is T) && (((T)parent).Uid == elementUid || string.IsNullOrEmpty(elementUid)))
{
return (T)parent;
}
parent = VisualTreeHelper.GetParent(parent);
}
return null;
}
/// <summary>
/// 根据控件名称,查找子控件
/// elementName为空时,查找指定类型的子控件
/// </summary>
public static T GetChildByUid<T>(this DependencyObject obj, string elementUid)
where T : FrameworkElement
{
DependencyObject child = null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Uid == elementUid) || (string.IsNullOrEmpty(elementUid)))
{
return (T)child;
}
else
{
T grandChild = GetChildByUid<T>(child, elementUid);
if (grandChild != null)
{
return grandChild;
}
}
}
return null;
}
/// <summary>
/// 根据控件名称,查找子控件集合
/// elementName为空时,查找指定类型的所有子控件
/// </summary>
public static List<T> GetChildsByUid<T>(this DependencyObject obj, string elementUid)
where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Uid == elementUid) || (string.IsNullOrEmpty(elementUid)))
{
childList.Add((T)child);
}
else
{
List<T> grandChildList = GetChildsByUid<T>(child, elementUid);
if (grandChildList != null)
{
childList.AddRange(grandChildList);
}
}
}
return childList;
}
}