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
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] DrawingBrush 엘리먼트 : GeometryDrawing 객체를 사용해 격자 배경 브러시 만들기 (0) | 2020.07.19 |
---|---|
[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] DependencyObject 클래스 : 타입으로 조상 찾기 (0) | 2020.07.18 |
[C#/WPF] DependencyObject 클래스 : 명칭으로 조상 찾기 (0) | 2020.07.18 |
[C#/WPF] DependencyObject 클래스 : 타입으로 조상 찾기 (0) | 2020.07.18 |