-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathVisualHelpers.cs
More file actions
74 lines (73 loc) · 2.57 KB
/
VisualHelpers.cs
File metadata and controls
74 lines (73 loc) · 2.57 KB
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
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace ColorCode.Common
{
public static class VisualHelpers
{
/// <summary>
/// Commandbars causing crashes at the mo
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parent"></param>
/// <param name="deepscan"></param>
/// <param name="includeCommandBars"></param>
/// <returns></returns>
public static T FirstChildofType<T>(DependencyObject parent, bool deepscan = false, bool includeCommandBars = false)
{
if (parent == null) return (T)(object)null;
if (deepscan)
{
if (parent is ContentPresenter presenter)
{
var _Child = presenter.Content as DependencyObject;
if (_Child is T)
{
return (T)(object)_Child;
}
if (!(_Child is CommandBar) || includeCommandBars)
{
var next = (FirstChildofType<T>(_Child, deepscan));
if (next is T)
{
return ((T)(object)next);
}
}
}
else if (parent is ContentControl control)
{
var _Child = control.Content as DependencyObject;
if (_Child is T)
{
return (T)(object)_Child;
}
if (!(_Child is CommandBar) || includeCommandBars)
{
var next = (FirstChildofType<T>(_Child, deepscan));
if (next is T)
{
return ((T)(object)next);
}
}
}
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is T)
{
return ((T)(object)_Child);
}
if (!(_Child is CommandBar) || includeCommandBars)
{
var next = (FirstChildofType<T>(_Child, deepscan));
if (next is T)
{
return ((T)(object)next);
}
}
}
return (T)(object)null;
}
}
}