2025-11-15 14:52:01 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
|
|
|
|
namespace Wino.Helpers;
|
|
|
|
|
|
|
|
|
|
public static class WinoVisualTreeHelper
|
|
|
|
|
{
|
2026-02-27 20:12:43 +01:00
|
|
|
public static T? GetChildObject<T>(DependencyObject? obj, string name) where T : FrameworkElement
|
2025-11-15 14:52:01 +01:00
|
|
|
{
|
2026-02-27 20:12:43 +01:00
|
|
|
if (obj == null) return null;
|
|
|
|
|
|
|
|
|
|
DependencyObject? child = null;
|
|
|
|
|
T? grandChild = null;
|
2025-11-15 14:52:01 +01:00
|
|
|
|
|
|
|
|
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
|
|
|
|
|
{
|
|
|
|
|
child = VisualTreeHelper.GetChild(obj, i);
|
|
|
|
|
|
2026-02-27 20:12:43 +01:00
|
|
|
if (child is T typedChild && (typedChild.Name == name || string.IsNullOrEmpty(name)))
|
2025-11-15 14:52:01 +01:00
|
|
|
{
|
2026-02-27 20:12:43 +01:00
|
|
|
return typedChild;
|
2025-11-15 14:52:01 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
grandChild = GetChildObject<T>(child, name);
|
|
|
|
|
}
|
|
|
|
|
if (grandChild != null)
|
|
|
|
|
{
|
|
|
|
|
return grandChild;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 20:12:43 +01:00
|
|
|
public static IEnumerable<T> FindDescendants<T>(this DependencyObject? depObj) where T : DependencyObject
|
2025-11-15 14:52:01 +01:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|