//public static IEnumerable<DependencyObject> Descendents(this DependencyObject root, Type descendentsType) //{ // return root.Descendents().Where(i => i.GetType() == descendentsType).Cast<DependencyObject>(); //} #endregion #region Elements /// <summary> /// Returns a collection of child elements. /// </summary> public static IEnumerable <DependencyObject> Elements(this DependencyObject item) { ILinqVisualTree <DependencyObject> adapter = new VisualTreeAdapter(item); foreach (var child in adapter.Children()) { yield return(child); } }
/// <summary> /// Returns a collection of ancestor elements. /// </summary> public static IEnumerable <DependencyObject> Ancestors(this DependencyObject item) { ILinqVisualTree <DependencyObject> adapter = new VisualTreeAdapter(item); var parent = adapter.Parent; while (parent != null) { yield return(parent); adapter = new VisualTreeAdapter(parent); parent = adapter.Parent; } }