public static IEnumerable <DependencyObject> Descendants(this DependencyObject obj) { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } var children = ArrayPool <DependencyObject> .Shared.Rent(obj.MaximumChildrenCount()); try { var childrenCount = obj.Children(children); for (var i = 0; i != childrenCount; ++i) { var child = children[i]; yield return(child); foreach (var grandChild in child.Descendants()) { yield return(grandChild); } } } finally { ArrayPool <DependencyObject> .Shared.Return(children); } }
/// <summary> /// 特定の型の子要素を取得 /// </summary> /// <param name="obj">起点となるオブジェクト</param> /// <returns>子要素</returns> public static IEnumerable <T> Children <T>(this DependencyObject obj) where T : DependencyObject { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } return(obj.Children().OfType <T>()); }
public static IEnumerable <DependencyObject> Descendants(this DependencyObject obj) { if (obj == null) { throw new ArgumentNullException(); } foreach (var child in obj.Children()) { yield return(child); foreach (var grandChild in child.Descendants()) { yield return(grandChild); } } }
private static DependencyObject GetInfoBubble(DependencyObject parent) { if (parent.GetType().Name == nameof(InfoBubbleView)) { return(parent); } foreach (var child in parent.Children()) { var output = GetInfoBubble(child); if (output != null) { return(output); } } return(null); }
public static IEnumerable <T> ChildrenOfType <T>(this DependencyObject parent) where T : DependencyObject { foreach (var child in parent.Children()) { var childType = child as T; if (childType == null) { foreach (var ele in ChildrenOfType <T>(child)) { yield return(ele); } } else { yield return(childType); } } }
public static IEnumerable <T> Children <T>(this DependencyObject obj) where T : DependencyObject { return(obj.Children().OfType <T>()); }
/* TODO In GetFocusableControls, assess if it is possible to use Linq and extension * methods provided by this class: * * return dependencyObject.Children<Control>() * .Where(x => x.IsKeyboardFocusable()) * .OrderBy(x => x.TabIndex); * * Steven Volckaert. September 26, 2013. */ /// <summary> /// Returns the DependencyObject's child System.Windows.FrameworkElement by examining the visual tree. /// </summary> /// <typeparam name="T">The type of the child element, which must inherit System.Windows.FrameworkElement.</typeparam> /// <param name="dependencyObject">The DependencyObject instance that this extension method affects.</param> /// <param name="name">The identifying name of the child FrameworkElement.</param> /// <returns>The child FrameworkElement, or <c>null</c> if it does not exist.</returns> public static T Child <T>(this DependencyObject dependencyObject, string name) where T : FrameworkElement { return(dependencyObject.Children <T>().FirstOrDefault(x => x.Name == name)); }
public static IEnumerable <DependencyObject> Descendants(this DependencyObject view) { return(view.Children().Flat(Children)); }