blob: 7f3ac3284ae176f89ac92630dd74c124f80815ba (
plain)
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
|
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace Xamarin.PropertyEditing.Windows
{
internal static class XamlHelper
{
public static IEnumerable<T> GetDescendants<T> (this UIElement self)
where T : UIElement
{
if (self == null)
throw new ArgumentNullException (nameof (self));
var childCount = VisualTreeHelper.GetChildrenCount (self);
for (var i = 0; i < childCount; i++) {
if (VisualTreeHelper.GetChild (self, i) is UIElement child) {
if (child is T correctlyTypedChild) yield return correctlyTypedChild;
IEnumerable<T> grandChildren = GetDescendants<T> (child);
foreach (T grandChild in grandChildren) yield return grandChild;
}
}
}
public static T FindParent<T> (this UIElement self)
where T : UIElement
{
UIElement parent = VisualTreeHelper.GetParent (self) as UIElement;
if (parent == null)
return default(T);
return parent.FindParentOrSelf<T>();
}
public static FrameworkElement FindPropertiesHost (this FrameworkElement self)
{
DependencyObject parent = self;
while (!(parent is IPropertiesHost) && parent != null) {
parent = VisualTreeHelper.GetParent (parent);
}
return (FrameworkElement)parent;
}
public static T FindParentOrSelf<T> (this UIElement self)
where T : UIElement
{
if (self == null)
throw new ArgumentNullException (nameof (self));
DependencyObject parent = self;
while (!(parent is T) && parent != null) {
parent = VisualTreeHelper.GetParent (parent);
}
return (T)parent;
}
public static TParent FindParentUnless<TParent,TUnless> (this UIElement self)
where TParent : UIElement
where TUnless : UIElement
{
if (self == null)
throw new ArgumentNullException (nameof (self));
DependencyObject parent = VisualTreeHelper.GetParent (self);
while (!(parent is TParent) && parent != null) {
if (parent is TUnless)
return default(TParent);
parent = VisualTreeHelper.GetParent (parent);
}
return (TParent)parent;
}
}
}
|