using System; using System.Linq; using System.Collections.Generic; using System.Windows; using System.Windows.Media; namespace LinqToVisualTree { /// /// Adapts a DependencyObject to provide methods required for generate /// a Linq To Tree API /// public class VisualTreeAdapter : ILinqTree { private DependencyObject _item; public VisualTreeAdapter(DependencyObject item) { _item = item; } public IEnumerable Children() { int childrenCount = VisualTreeHelper.GetChildrenCount(_item); for (int i = 0; i < childrenCount; i++) { yield return VisualTreeHelper.GetChild(_item, i); } } public DependencyObject Parent { get { return VisualTreeHelper.GetParent(_item); } } } } namespace LinqToVisualTree { /// /// Defines an interface that must be implemented to generate the LinqToTree methods /// /// public interface ILinqTree { IEnumerable Children(); T Parent { get; } } public static class TreeExtensions { /// /// Returns a collection of descendant elements. /// public static IEnumerable Descendants(this DependencyObject item) { ILinqTree adapter = new VisualTreeAdapter(item); foreach (var child in adapter.Children()) { yield return child; foreach (var grandChild in child.Descendants()) { yield return grandChild; } } } /// /// Returns a collection containing this element and all descendant elements. /// public static IEnumerable DescendantsAndSelf(this DependencyObject item) { yield return item; foreach (var child in item.Descendants()) { yield return child; } } /// /// Returns a collection of ancestor elements. /// public static IEnumerable Ancestors(this DependencyObject item) { ILinqTree adapter = new VisualTreeAdapter(item); var parent = adapter.Parent; while (parent != null) { yield return parent; adapter = new VisualTreeAdapter(parent); parent = adapter.Parent; } } /// /// Returns a collection containing this element and all ancestor elements. /// public static IEnumerable AncestorsAndSelf(this DependencyObject item) { yield return item; foreach (var ancestor in item.Ancestors()) { yield return ancestor; } } /// /// Returns a collection of child elements. /// public static IEnumerable Elements(this DependencyObject item) { ILinqTree adapter = new VisualTreeAdapter(item); foreach (var child in adapter.Children()) { yield return child; } } /// /// Returns a collection of the sibling elements before this node, in document order. /// public static IEnumerable ElementsBeforeSelf(this DependencyObject item) { if (item.Ancestors().FirstOrDefault() == null) yield break; foreach (var child in item.Ancestors().First().Elements()) { if (child.Equals(item)) break; yield return child; } } /// /// Returns a collection of the after elements after this node, in document order. /// public static IEnumerable ElementsAfterSelf(this DependencyObject item) { if (item.Ancestors().FirstOrDefault() == null) yield break; bool afterSelf = false; foreach (var child in item.Ancestors().First().Elements()) { if (afterSelf) yield return child; if (child.Equals(item)) afterSelf = true; } } /// /// Returns a collection containing this element and all child elements. /// public static IEnumerable ElementsAndSelf(this DependencyObject item) { yield return item; foreach (var child in item.Elements()) { yield return child; } } /// /// Returns a collection of descendant elements which match the given type. /// public static IEnumerable Descendants(this DependencyObject item) { return item.Descendants().Where(i => i is T).Cast(); } /// /// Returns a collection of the sibling elements before this node, in document order /// which match the given type. /// public static IEnumerable ElementsBeforeSelf(this DependencyObject item) { return item.ElementsBeforeSelf().Where(i => i is T).Cast(); } /// /// Returns a collection of the after elements after this node, in document order /// which match the given type. /// public static IEnumerable ElementsAfterSelf(this DependencyObject item) { return item.ElementsAfterSelf().Where(i => i is T).Cast(); } /// /// Returns a collection containing this element and all descendant elements /// which match the given type. /// public static IEnumerable DescendantsAndSelf(this DependencyObject item) { return item.DescendantsAndSelf().Where(i => i is T).Cast(); } /// /// Returns a collection of ancestor elements which match the given type. /// public static IEnumerable Ancestors(this DependencyObject item) { return item.Ancestors().Where(i => i is T).Cast(); } /// /// Returns a collection containing this element and all ancestor elements /// which match the given type. /// public static IEnumerable AncestorsAndSelf(this DependencyObject item) { return item.AncestorsAndSelf().Where(i => i is T).Cast(); } /// /// Returns a collection of child elements which match the given type. /// public static IEnumerable Elements(this DependencyObject item) { return item.Elements().Where(i => i is T).Cast(); } /// /// Returns a collection containing this element and all child elements. /// which match the given type. /// public static IEnumerable ElementsAndSelf(this DependencyObject item) { return item.ElementsAndSelf().Where(i => i is T).Cast(); } } public static class EnumerableTreeExtensions { /// /// Applies the given function to each of the items in the supplied /// IEnumerable. /// private static IEnumerable DrillDown(this IEnumerable items, Func> function) { foreach (var item in items) { foreach (var itemChild in function(item)) { yield return itemChild; } } } /// /// Applies the given function to each of the items in the supplied /// IEnumerable, which match the given type. /// public static IEnumerable DrillDown(this IEnumerable items, Func> function) where T : DependencyObject { foreach (var item in items) { foreach (var itemChild in function(item)) { if (itemChild is T) { yield return (T)itemChild; } } } } /// /// Returns a collection of descendant elements. /// public static IEnumerable Descendants(this IEnumerable items) { return items.DrillDown(i => i.Descendants()); } /// /// Returns a collection containing this element and all descendant elements. /// public static IEnumerable DescendantsAndSelf(this IEnumerable items) { return items.DrillDown(i => i.DescendantsAndSelf()); } /// /// Returns a collection of ancestor elements. /// public static IEnumerable Ancestors(this IEnumerable items) { return items.DrillDown(i => i.Ancestors()); } /// /// Returns a collection containing this element and all ancestor elements. /// public static IEnumerable AncestorsAndSelf(this IEnumerable items) { return items.DrillDown(i => i.AncestorsAndSelf()); } /// /// Returns a collection of child elements. /// public static IEnumerable Elements(this IEnumerable items) { return items.DrillDown(i => i.Elements()); } /// /// Returns a collection containing this element and all child elements. /// public static IEnumerable ElementsAndSelf(this IEnumerable items) { return items.DrillDown(i => i.ElementsAndSelf()); } /// /// Returns a collection of descendant elements which match the given type. /// public static IEnumerable Descendants(this IEnumerable items) where T : DependencyObject { return items.DrillDown(i => i.Descendants()); } /// /// Returns a collection containing this element and all descendant elements. /// which match the given type. /// public static IEnumerable DescendantsAndSelf(this IEnumerable items) where T : DependencyObject { return items.DrillDown(i => i.DescendantsAndSelf()); } /// /// Returns a collection of ancestor elements which match the given type. /// public static IEnumerable Ancestors(this IEnumerable items) where T : DependencyObject { return items.DrillDown(i => i.Ancestors()); } /// /// Returns a collection containing this element and all ancestor elements. /// which match the given type. /// public static IEnumerable AncestorsAndSelf(this IEnumerable items) where T : DependencyObject { return items.DrillDown(i => i.AncestorsAndSelf()); } /// /// Returns a collection of child elements which match the given type. /// public static IEnumerable Elements(this IEnumerable items) where T : DependencyObject { return items.DrillDown(i => i.Elements()); } /// /// Returns a collection containing this element and all child elements. /// which match the given type. /// public static IEnumerable ElementsAndSelf(this IEnumerable items) where T : DependencyObject { return items.DrillDown(i => i.ElementsAndSelf()); } } }