54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using Windows.UI.Xaml;
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
namespace Wino.Helpers;
|
|
|
|
public static class WinoVisualTreeHelper
|
|
{
|
|
public static T GetChildObject<T>(DependencyObject obj, string name) where T : FrameworkElement
|
|
{
|
|
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 && (((T)child).Name == name | string.IsNullOrEmpty(name)))
|
|
{
|
|
return (T)child;
|
|
}
|
|
else
|
|
{
|
|
grandChild = GetChildObject<T>(child, name);
|
|
}
|
|
if (grandChild != null)
|
|
{
|
|
return grandChild;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static IEnumerable<T> FindDescendants<T>(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<T>(child))
|
|
{
|
|
yield return childOfChild;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|