This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 757
/
VisualTreeHelperEx.cs
116 lines (107 loc) · 4.16 KB
/
VisualTreeHelperEx.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
namespace FirstFloor.ModernUI.Windows.Media
{
/// <summary>
/// Provides addition visual tree helper methods.
/// </summary>
public static class VisualTreeHelperEx
{
/// <summary>
/// Gets specified visual state group.
/// </summary>
/// <param name="dependencyObject">The dependency object.</param>
/// <param name="groupName">Name of the group.</param>
/// <returns></returns>
public static VisualStateGroup TryGetVisualStateGroup(this DependencyObject dependencyObject, string groupName)
{
FrameworkElement root = GetImplementationRoot(dependencyObject);
if (root == null) {
return null;
}
return (from @group in VisualStateManager.GetVisualStateGroups(root).OfType<VisualStateGroup>()
where string.CompareOrdinal(groupName, @group.Name) == 0
select @group).FirstOrDefault<VisualStateGroup>();
}
/// <summary>
/// Gets the implementation root.
/// </summary>
/// <param name="dependencyObject">The dependency object.</param>
/// <returns></returns>
public static FrameworkElement GetImplementationRoot(this DependencyObject dependencyObject)
{
if (1 != VisualTreeHelper.GetChildrenCount(dependencyObject)) {
return null;
}
return (VisualTreeHelper.GetChild(dependencyObject, 0) as FrameworkElement);
}
/// <summary>
/// Returns a collection of the visual ancestor elements of specified dependency object.
/// </summary>
/// <param name="dependencyObject">The dependency object.</param>
/// <returns>
/// A collection that contains the ancestors elements.
/// </returns>
public static IEnumerable<DependencyObject> Ancestors(this DependencyObject dependencyObject)
{
var parent = dependencyObject;
while (true) {
parent = GetParent(parent);
if (parent != null) {
yield return parent;
}
else {
break;
}
}
}
/// <summary>
/// Returns a collection of visual elements that contain specified object, and the ancestors of specified object.
/// </summary>
/// <param name="dependencyObject">The dependency object.</param>
/// <returns>
/// A collection that contains the ancestors elements and the object itself.
/// </returns>
public static IEnumerable<DependencyObject> AncestorsAndSelf(this DependencyObject dependencyObject)
{
if (dependencyObject == null) {
throw new ArgumentNullException("dependencyObject");
}
var parent = dependencyObject;
while (true) {
if (parent != null) {
yield return parent;
}
else {
break;
}
parent = GetParent(parent);
}
}
/// <summary>
/// Gets the parent for specified dependency object.
/// </summary>
/// <param name="dependencyObject">The dependency object</param>
/// <returns>The parent object or null if there is no parent.</returns>
public static DependencyObject GetParent(this DependencyObject dependencyObject)
{
if (dependencyObject == null) {
throw new ArgumentNullException("dependencyObject");
}
var ce = dependencyObject as ContentElement;
if (ce != null) {
var parent = ContentOperations.GetParent(ce);
if (parent != null) {
return parent;
}
var fce = ce as FrameworkContentElement;
return fce != null ? fce.Parent : null;
}
return VisualTreeHelper.GetParent(dependencyObject);
}
}
}