Files
Wino-Mail/Wino.Core.UWP/Helpers/WinoVisualTreeHelper.cs

54 lines
1.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2024-04-18 01:44:37 +02:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
2025-02-16 11:54:23 +01:00
namespace Wino.Helpers;
public static class WinoVisualTreeHelper
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
public static T GetChildObject<T>(DependencyObject obj, string name) where T : FrameworkElement
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
DependencyObject child = null;
T grandChild = null;
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
child = VisualTreeHelper.GetChild(obj, i);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (child is T && (((T)child).Name == name | string.IsNullOrEmpty(name)))
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
return (T)child;
}
else
{
grandChild = GetChildObject<T>(child, name);
}
if (grandChild != null)
{
return grandChild;
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
return null;
}
2025-02-16 11:54:23 +01:00
public static IEnumerable<T> FindDescendants<T>(this DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
2025-02-16 11:54:23 +01:00
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
2025-02-16 11:54:23 +01:00
var child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
2025-02-16 11:54:23 +01:00
yield return (T)child;
}
2025-02-16 11:54:23 +01:00
foreach (T childOfChild in FindDescendants<T>(child))
{
yield return childOfChild;
}
}
}
2024-04-18 01:44:37 +02:00
}
}