using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
namespace MultiTerm.Wpf.CustomControl;
public static class UIHelper
{
/// from https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type
///
/// Finds a parent of a given item on the visual tree.
///
/// The type of the queried item.
/// A direct or indirect child of the queried item.
/// The first parent item that matches the submitted type parameter.
/// If not matching item can be found, a null reference is being returned.
public static T? FindVisualParent(DependencyObject child)
where T : DependencyObject
{
// get parent item
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
// we’ve reached the end of the tree
if (parentObject == null) return null;
// check if the parent matches the type we’re looking for
if (parentObject is T parent)
{
return parent;
}
else
{
// use recursion to proceed with next level
return FindVisualParent(parentObject);
}
}
}