■ VisualTreeHelper 클래스 : 조상 비주얼 객체 구하기

------------------------------------------------------------------------------------------------------------------------

using System.Windows;

using System.Windows.Media;

 

#region 조상 비주얼 객체 구하기 - GetAncesterVisualObject<T>(sourceObject, allowSubclass)

 

/// <summary>

/// 조상 비주얼 객체 구하기

/// </summary>

/// <typeparam name="T">조상 비주얼 타입</typeparam>

/// <param name="sourceObject">소스 객체</param>

/// <param name="allowSubclass">서브 클래스 허용 여부</param>

/// <returns>조상 비주얼 객체</returns>

public T GetAncesterVisualObject<T>(DependencyObject sourceObject, bool allowSubclass = false) where T : DependencyObject

{

    DependencyObject parentDependencyObject = VisualTreeHelper.GetParent(sourceObject);

 

    if(parentDependencyObject != null)

    {

        if(parentDependencyObject.GetType() == typeof(T) || (allowSubclass && parentDependencyObject is T))

        {

            return (T)parentDependencyObject;

        }

        else

        {

            return GetAncesterVisualObject<T>(parentDependencyObject, allowSubclass);

        }

    }

 

    return null;

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요