■ Visual 클래스 : 타입으로 자손 찾기
------------------------------------------------------------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Media;
#region 타입으로 자손 찾기 - FindDescendantByType(source, descendantType, specificTypeOnly)
/// <summary>
/// 타입으로 자손 찾기
/// </summary>
/// <param name="source">소스 객체</param>
/// <param name="descendantType">자손 타입</param>
/// <param name="specificTypeOnly">특정 타입만 찾기 여부</param>
/// <returns>자손 객체</returns>
public Visual FindDescendantByType(Visual source, Type descendantType, bool specificTypeOnly)
{
if(source == null)
{
return null;
}
if(specificTypeOnly ? (source.GetType() == descendantType) : (source.GetType() == descendantType) || (source.GetType().IsSubclassOf(descendantType)))
{
return source;
}
Visual target = null;
if(source is FrameworkElement)
{
(source as FrameworkElement).ApplyTemplate();
}
for(int i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++)
{
Visual visual = VisualTreeHelper.GetChild(source, i) as Visual;
target = FindDescendantByType(visual, descendantType, specificTypeOnly);
if(target != null)
{
break;
}
}
return target;
}
#endregion
#region 타입으로 자손 찾기 - FindDescendantByType(source, descendantType)
/// <summary>
/// 타입으로 자손 찾기
/// </summary>
/// <param name="source">소스 객체</param>
/// <param name="descendantType">자손 타입</param>
/// <returns>자손 객체</returns>
public Visual FindDescendantByType(Visual source, Type descendantType)
{
return FindDescendantByType(source, descendantType, true);
}
#endregion
#region 타입으로 자손 찾기 - FindDescendantByType<TDescendant>(source)
/// <summary>
/// 타입으로 자손 찾기
/// </summary>
/// <typeparam name="TDescendant">자손 타입</typeparam>
/// <param name="source">소스 객체</param>
/// <returns>자손 객체</returns>
public TDescendant FindDescendantByType<TDescendant>(Visual source) where TDescendant : Visual
{
Visual target = VisualHelper.FindDescendantByType(source, typeof(TDescendant));
return (TDescendant)target;
}
#endregion
------------------------------------------------------------------------------------------------------------------------
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Thumb 클래스 : 객체 이동하기/크기 조정하기/회전하기 (0) | 2020.07.18 |
---|---|
[C#/WPF] Thumb 클래스 : 객체 이동하기/크기 조정하기 (0) | 2020.07.18 |
[C#/WPF] DragDrop 클래스 : DoDragDrop 정적 메소드를 사용해 드래그 & 드롭 사용하기 (0) | 2020.07.18 |
[C#/WPF] ICommand 인터페이스 : 명령 명칭으로 실행하기 (0) | 2020.07.18 |
[C#/WPF] Visual 클래스 : 속성 값을 갖는 자손 찾기 (0) | 2020.07.18 |
[C#/WPF] Visual 클래스 : 타입으로 자손 찾기 (0) | 2020.07.18 |
[C#/WPF] Visual 클래스 : 명칭으로 자손 찾기 (0) | 2020.07.18 |
[C#/WPF] DependencyObject 클래스 : 타입으로 조상 찾기 (0) | 2020.07.18 |
[C#/WPF] DependencyObject 클래스 : 명칭으로 조상 찾기 (0) | 2020.07.18 |
[C#/WPF] DependencyObject 클래스 : 타입으로 조상 찾기 (0) | 2020.07.18 |
[C#/WPF] Canvas 클래스 : 논리적 단위 캔버스 사용하기 (0) | 2020.07.18 |
댓글을 달아 주세요