using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace OwlCore.WinUI.Extensions.Windows.UI.Xaml
{
///
/// A collection of extension methods for
///
public static class UIElementExtensions
{
///
/// Finds the first child of a specified type on a given .
///
/// The type to look for.
/// The object to search through.
/// The located object, if found.
public static T FindChild(this DependencyObject parent) where T : class
{
if (parent is T tParent)
{
return tParent;
}
var childCount = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < childCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T tChild)
{
return tChild;
}
var tFromChild = child.FindChild();
if (tFromChild != null)
{
return tFromChild;
}
}
return null!;
}
}
}