Files
Example/RazorEngineTest/ExHelper.cs

30 lines
953 B
C#
Raw Normal View History

2025-09-30 08:21:01 +08:00
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<T>(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<T>(name);
if (descendant != null) return descendant;
}
return null;
}
}
}