using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using System.Windows; namespace ComPDFKit.Tool.Help { public static class CommonHelper { /// /// Find the parent control of the target type of the object /// /// /// /// public static T FindVisualParent(DependencyObject obj) where T : class { try { while (obj != null) { if (obj is T) return obj as T; obj = VisualTreeHelper.GetParent(obj); } return null; } catch { return null; } } /// /// Find the child control of the target type of the object /// /// /// The type of the child control to find /// /// /// The object to find /// /// /// The child control of the target type of the object /// public static childItem FindVisualChild(DependencyObject obj) where childItem : DependencyObject { try { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild(child); if (childOfChild != null) return childOfChild; } } return null; } catch { return null; } } /// /// Find the child control of the target type of the object /// /// /// The type of the child control to find /// /// /// The object to find /// /// /// The child control of the target type of the object /// public static List FindVisualChildList(DependencyObject obj) where childItem : DependencyObject { List children = new List (); try { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is childItem) { children.Add((childItem)child); } else { childItem childOfChild = FindVisualChild(child); if (childOfChild != null) children.Add(childOfChild); } } return children; } catch { return children; } } } }