/// <summary> /// Returns a collection of the sibling elements before this node, in document order. /// </summary> public static IEnumerable <DependencyObject> 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); } }
/// <summary> /// Returns a collection containing this element and all ancestor elements. /// </summary> public static IEnumerable <DependencyObject> AncestorsAndSelf(this DependencyObject item) { yield return(item); foreach (var ancestor in item.Ancestors()) { yield return(ancestor); } }
/// <summary> /// Returns a collection of the after elements after this node, in document order. /// </summary> public static IEnumerable <DependencyObject> 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; } } }
public static T AncestorsFind <T>(this DependencyObject item) where T : DependencyObject { return(item.Ancestors <T>().FirstOrDefault() as T); }
/// <summary> /// Returns a collection of ancestor elements which match the given type. /// </summary> public static IEnumerable <DependencyObject> Ancestors <T>(this DependencyObject item) { return(item.Ancestors().Where(i => i is T)); }