/// <summary> /// Really a helper for FindDownInTree, typically not called directly. /// </summary> /// <param name="lst"></param> /// <param name="parent"></param> /// <param name="ignore"></param> /// <param name="helper"></param> public static void FindDownInTree(List<FrameworkElement> lst, DependencyObject parent, DependencyObject ignore, IFinderMatchVisualHelper helper) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { if (lst.Count > 0 && helper.StopAfterFirst) break; DependencyObject visual = VisualTreeHelper.GetChild(parent, i); if (visual is FrameworkElement) { (visual as FrameworkElement).ApplyTemplate(); } if (helper.DoesMatch(visual)) { lst.Add(visual as FrameworkElement); } if (lst.Count > 0 && helper.StopAfterFirst) break; if (visual != ignore) { FindDownInTree(lst, visual, ignore, helper); } } }
/// <summary> /// Really a helper to look Up in a tree, typically not called directly. /// </summary> /// <param name="lst"></param> /// <param name="parent"></param> /// <param name="ignore"></param> /// <param name="helper"></param> public static void FindUpInTree(List<FrameworkElement> lst, Visual parent, Visual ignore, IFinderMatchVisualHelper helper) { // First thing to do is find Down in the existing node... //FindDownInTree(lst, parent, ignore, helper); if (helper.DoesMatch(parent)) { lst.Add(parent as FrameworkElement); } // Ok, now check to see we are not at a stop.. i.e. got it. if (lst.Count > 0 && helper.StopAfterFirst) { // Hum, don't think we need to do anything here: yet. } else { // Ok, now try to get a new parent... FrameworkElement feCast = parent as FrameworkElement; if (feCast != null) { FrameworkElement feNewParent = feCast.Parent as FrameworkElement; if (feNewParent == null || feNewParent == feCast) { // Try to get the templated parent feNewParent = feCast.TemplatedParent as FrameworkElement; } // Now check to see that we have a valid parent if (feNewParent != null && feNewParent != feCast) { // Pass up FindUpInTree(lst, feNewParent, feCast, helper); } } } }
/// <summary> /// Walker that looks both UP and down in the visual tree for any matching elements, typically used with Type /// </summary> /// <param name="parent">Start point in the tree to search</param> /// <param name="helper">Match Helper to use</param> /// <returns>List of matching FrameworkElements</returns> public static List<FrameworkElement> FindInTree(Visual parent, IFinderMatchVisualHelper helper) { List<FrameworkElement> lst = new List<FrameworkElement>(); FindUpInTree(lst, parent, null, helper); return lst; }
/// <summary> /// All way visual tree helper that searches UP and DOWN in a tree for the matching pattern. /// /// This is used to walk for name matches or type matches typically. /// /// Returns only the first matching element /// </summary> /// <param name="parent">Start point in the tree to search</param> /// <param name="helper">Match Helper to use</param> /// <returns>Null if no match, else returns the first element that matches</returns> public static FrameworkElement SingleFindInTree(Visual parent, IFinderMatchVisualHelper helper) { helper.StopAfterFirst = true; List<FrameworkElement> lst = FindInTree(parent, helper); FrameworkElement feRet = null; if (lst.Count > 0) feRet = lst[0]; return feRet; }
/// <summary> /// Really a helper for FindDownInTree, typically not called directly. /// </summary> /// <param name="lst"></param> /// <param name="parent"></param> /// <param name="ignore"></param> /// <param name="helper"></param> public static void FindDownInTree(List <FrameworkElement> lst, DependencyObject parent, DependencyObject ignore, IFinderMatchVisualHelper helper) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { if (lst.Count > 0 && helper.StopAfterFirst) { break; } DependencyObject visual = VisualTreeHelper.GetChild(parent, i); if (visual is FrameworkElement) { (visual as FrameworkElement).ApplyTemplate(); } if (helper.DoesMatch(visual)) { lst.Add(visual as FrameworkElement); } if (lst.Count > 0 && helper.StopAfterFirst) { break; } if (visual != ignore) { FindDownInTree(lst, visual, ignore, helper); } } }
/// <summary> /// Really a helper to look Up in a tree, typically not called directly. /// </summary> /// <param name="lst"></param> /// <param name="parent"></param> /// <param name="ignore"></param> /// <param name="helper"></param> public static void FindUpInTree(List <FrameworkElement> lst, Visual parent, Visual ignore, IFinderMatchVisualHelper helper) { // First thing to do is find Down in the existing node... //FindDownInTree(lst, parent, ignore, helper); if (helper.DoesMatch(parent)) { lst.Add(parent as FrameworkElement); } // Ok, now check to see we are not at a stop.. i.e. got it. if (lst.Count > 0 && helper.StopAfterFirst) { // Hum, don't think we need to do anything here: yet. } else { // Ok, now try to get a new parent... FrameworkElement feCast = parent as FrameworkElement; if (feCast != null) { FrameworkElement feNewParent = feCast.Parent as FrameworkElement; if (feNewParent == null || feNewParent == feCast) { // Try to get the templated parent feNewParent = feCast.TemplatedParent as FrameworkElement; } // Now check to see that we have a valid parent if (feNewParent != null && feNewParent != feCast) { // Pass up FindUpInTree(lst, feNewParent, feCast, helper); } } } }
public static void FindUpInTree(List <FrameworkElement> lst, Visual parent, Visual ignore, IFinderMatchVisualHelper helper) { if (helper.DoesMatch(parent)) { lst.Add((FrameworkElement)parent); } if (!(lst.Count > 0 && helper.FindFirst) && parent is FrameworkElement element) { FrameworkElement nParent = (FrameworkElement)element.Parent; if (nParent == null || nParent == element) { nParent = (FrameworkElement)element.TemplatedParent; } if (nParent != null && nParent != element) { WpfHelper.FindUpInTree(lst, nParent, element, helper); } } }