C#/WPF

[WPF] Find Parent

딸기우유중독 2023. 5. 31. 16:56

 

    private static T FindParent<T>(DependencyObject child, string parentName)
        where T : DependencyObject
    {
        if (child == null) return null;

        T foundParent = null;
        var currentParent = VisualTreeHelper.GetParent(child);

        do
        {
            var frameworkElement = currentParent as FrameworkElement;
            if(frameworkElement.Name == parentName && frameworkElement is T)
            {
                foundParent = (T) currentParent;
                break;
            }

            currentParent = VisualTreeHelper.GetParent(currentParent);

        } while (currentParent != null);

        return foundParent;
    }

 

 


https://stackoverflow.com/questions/15198104/find-parent-of-control-by-name

 

Find Parent of Control by Name

Is there a way to find the parents of a WPF control by its name, when the name is set in the xaml code?

stackoverflow.com



 

728x90