첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ Visual 클래스를 사용해 속성 값을 갖는 자손을 찾는 방법을 보여준다.

 

▶ 예제 코드 (C#)

using System.Windows;
using System.Windows.Media;

#region 속성 값을 갖는 자손 찾기 - FindDescendantWithPropertyValue(source, property, value)

/// <summary>
/// 속성 값을 갖는 자손 찾기
/// </summary>
/// <param name="source">소스 객체</param>
/// <param name="property">속성</param>
/// <param name="value">값</param>
/// <returns>자손 객체</returns>
public Visual FindDescendantWithPropertyValue(Visual source, DependencyProperty property, object value)
{
    if(source == null)
    {
        return null;
    }

    if(source.GetValue(property) == null)
    {
        return null;
    }

    if(source.GetValue(property).Equals(value))
    {
        return source;
    }

    Visual target = null;

    if(source is FrameworkElement)
    {
        (source as FrameworkElement).ApplyTemplate();
    }

    for(int i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++)
    {
        Visual child = VisualTreeHelper.GetChild(source, i) as Visual;

        target = FindDescendantWithPropertyValue(child, property, value);

        if(target != null)
        {
            break;
        }
    }

    return target;
}

#endregion
728x90
그리드형(광고전용)
Posted by icodebroker
,