using System.Collections.Generic; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media; namespace Wino.Helpers; public static class WinoVisualTreeHelper { public static T? GetChildObject(DependencyObject? obj, string name) where T : FrameworkElement { if (obj == null) return null; DependencyObject? child = null; T? grandChild = null; for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++) { child = VisualTreeHelper.GetChild(obj, i); if (child is T typedChild && (typedChild.Name == name || string.IsNullOrEmpty(name))) { return typedChild; } else { grandChild = GetChildObject(child, name); } if (grandChild != null) { return grandChild; } } return null; } public static IEnumerable FindDescendants(this DependencyObject? depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { var child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindDescendants(child)) { yield return childOfChild; } } } } }