4 using System.Collections.Generic;
7 using System.Reflection;
8 using System.Windows.Media;
10 namespace SiliconStudio.Presentation.Extensions
23 throw new ArgumentNullException(
"source");
27 Type dependencyPropertyType = typeof(DependencyProperty);
29 BindingFlags
flags = BindingFlags.Public | BindingFlags.Static;
30 if (includingParentProperties)
31 flags |= BindingFlags.FlattenHierarchy;
33 return source.DependencyObjectType.SystemType.GetFields(
flags)
34 .Where(fi => fi.MemberType == MemberTypes.Field && fi.FieldType == dependencyPropertyType)
35 .Select(fi => (DependencyProperty)fi.GetValue(source))
36 .OrderBy(fi => fi.Name)
49 throw new ArgumentNullException(
"source");
51 throw new ArgumentNullException(
"property");
53 source.SetValue(property, value);
54 foreach (
object child
in LogicalTreeHelper.GetChildren(source as dynamic))
58 depChild.DeepSetValue(property, value);
71 throw new ArgumentNullException(
"source");
74 while (source != null)
76 root = source as Visual;
77 source = VisualTreeHelper.GetParent(source);
91 throw new ArgumentNullException(
"source");
93 return FindParentOfType<T>(source, VisualTreeHelper.GetParent);
105 throw new ArgumentNullException(
"source");
107 return FindChildOfType<T>(source, VisualTreeHelper.GetChildrenCount, VisualTreeHelper.GetChild);
119 throw new ArgumentNullException(
"source");
121 return FindChildrenOfType<T>(source, VisualTreeHelper.GetChildrenCount, VisualTreeHelper.GetChild);
131 int childrenCount = VisualTreeHelper.GetChildrenCount(source);
132 return childrenCount > 0 ? VisualTreeHelper.GetChild(source, 0) : null;
144 throw new ArgumentNullException(
"source");
146 return FindParentOfType<T>(source, LogicalTreeHelper.GetParent);
159 throw new ArgumentNullException(
"source");
161 return FindChildOfType<T>(source,
162 d => LogicalTreeHelper.GetChildren(d).Cast<DependencyObject>().Count(),
163 (d, i) => LogicalTreeHelper.GetChildren(d).Cast<DependencyObject>().ElementAt(i));
176 throw new ArgumentNullException(
"source");
178 return FindChildrenOfType<T>(source,
179 d => LogicalTreeHelper.GetChildren(d).Cast<DependencyObject>().Count(),
180 (d, i) => LogicalTreeHelper.GetChildren(d).Cast<DependencyObject>().ElementAt(i));
184 #region Helper methods
196 throw new ArgumentNullException(
"source");
197 if (getParentFunc == null)
198 throw new ArgumentNullException(
"getParentFunc");
201 var parent = getParentFunc(source);
212 return FindParentOfType<T>(parent, getParentFunc);
227 private static T FindChildOfType<T>(
DependencyObject source, Func<DependencyObject, int> getChildrenCountFunc, Func<DependencyObject, int, DependencyObject> getChildFunc) where T :
DependencyObject
230 throw new ArgumentNullException(
"source");
231 if (getChildrenCountFunc == null)
232 throw new ArgumentNullException(
"getChildrenCountFunc");
233 if (getChildFunc == null)
234 throw new ArgumentNullException(
"getChildFunc");
236 var childCount = getChildrenCountFunc(source);
237 for (var i = 0; i < childCount; i++)
239 var child = getChildFunc(source, i);
244 child = FindChildOfType<T>(child, getChildrenCountFunc, getChildFunc);
263 throw new ArgumentNullException(
"source");
264 if (getChildrenCountFunc == null)
265 throw new ArgumentNullException(
"getChildrenCountFunc");
266 if (getChildFunc == null)
267 throw new ArgumentNullException(
"getChildFunc");
269 var childCount = getChildrenCountFunc(source);
270 for (var i = 0; i < childCount; i++)
272 var child = getChildFunc(source, i);
276 yield
return child as T;
278 foreach (var subChild
in FindChildrenOfType<T>(child, getChildrenCountFunc, getChildFunc).Where(x => x != null))
280 yield
return subChild;
static DependencyProperty[] GetDependencyProperties(this DependencyObject source, bool includingParentProperties=false)
Retrieves the public static DependencyProperties.
static DependencyObject FindFirstVisualChild(this DependencyObject source)
Gets the first visual child of the given object.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ DXGI_FORMAT _In_ DWORD flags
static Visual FindVisualRoot(this DependencyObject source)
Find the root parent that match the given type, along the visual tree.
static void DeepSetValue(this DependencyObject source, DependencyProperty property, object value)
Sets the value of a DependencyProperty on a DependencyObject and all its logical children.