using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using System.Windows; namespace RazorEngineTest { public static class ExHelper { public static T? FindVisualChild(this DependencyObject parent, string name = "") where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); if (child is T result) { if (string.IsNullOrEmpty(name)) return result; else if (result is FrameworkElement element && element.Name == name) return result; } var descendant = child.FindVisualChild(name); if (descendant != null) return descendant; } return null; } } }