Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XamlHelper.cs « Xamarin.PropertyEditing.Windows - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ea99d5ab3c675becd45ec840699437d3a7c27d2 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 FindChildOrSelf<T> (this UIElement self)
			where T : UIElement
		{
			if (self == null)
				throw new ArgumentNullException (nameof(self));

			if (self is T t)
				return t;

			int count = VisualTreeHelper.GetChildrenCount (self);
			for (int i = 0; i < count; i++) {
				if (VisualTreeHelper.GetChild (self, i) is UIElement element) {
					var child = FindChildOrSelf<T> (element);
					if (child != null)
						return child;
				}
			}

			return null;
		}

		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;
		}
	}
}