-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControlUtilities.cs
54 lines (49 loc) · 1.94 KB
/
ControlUtilities.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
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace Ratio.UWP.Controls
{
public class ControlUtilities
{
public static DependencyObject RecursiveFindByType(DependencyObject dependencyObject, Type type, string name = null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
{
var depObj = VisualTreeHelper.GetChild(dependencyObject, i);
var frameworkElement = depObj as FrameworkElement;
if ((depObj.GetType() == type && name == null) || (name != null && name == frameworkElement?.Name)) return depObj;
var child = RecursiveFindByType(depObj, type, name);
if (child != null) return child;
}
return null;
}
public static DependencyObject LookUpTreeByType(DependencyObject dependencyObject, Type type, string name = null)
{
while (true)
{
var depObject = VisualTreeHelper.GetParent(dependencyObject);
if (depObject != null)
{
var fe = depObject as FrameworkElement;
if (fe != null)
{
if (fe.GetType() == type && (name != null && fe.Name == name)) return depObject;
}
dependencyObject = depObject;
continue;
}
return null;
}
}
public static void TraverseAndApply(DependencyObject dependencyObject, Action<DependencyObject> action)
{
var count = VisualTreeHelper.GetChildrenCount(dependencyObject);
for (var i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(dependencyObject, i);
action(child);
TraverseAndApply(child,action);
}
}
}
}