2011-01-04 4 views

답변

4

이 작업을 수행하려면 VisualTreeHelper을 사용할 수 있습니다. 그런 다음 다음을 수행 할 수 있습니다

public static IEnumerable<DependencyObject> GetVisuals(this DependencyObject root) 
{ 
    int count = VisualTreeHelper.GetChildrenCount(root); 
    for (int i = 0; i < count; i++) 
    { 
     var child = VisualTreeHelper.GetChild(root, i); 
     yield return child; 
     foreach (var descendants in child.GetVisuals()) 
     { 
      yield return descendants; 
     } 
    } 
} 

:

는 다음과 같은 확장 방법은 도움을 제공하는 기사 here있다

foreach (var control in LayoutRoot.GetVisuals().OfType<Control>()) 
{ 
    //handle control of type <Control> 
} 
1

BrokenGlass의 방법은 확실히 작동하지만, 조금 둔한입니다. 다음은이를 수행하는 간단한 방법입니다.

VisualStudio 또는 Blend에서 컨트롤을 만들 때 일반적으로 내부에 눈금이있는 UserControl을 가져옵니다. 당신의 UserControl의 루트 요소를 가정

<UserControl x:Class="RHooligan" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300"> 
<Grid x:Name="RootElement> 

</Grid> 

컨테이너 컨트롤 (그리드, StackPanel의, 캔버스 등) 그리고 당신은 그것을 RootElement, 당신은 그것의 아이들을 반복하는이 작업을 수행 할 수 있습니다 명명했습니다.

 foreach(FrameworkElement element in RootElement.Children) 
    { 
    //do something with element 
    ////////////////////////// 
    } 
관련 문제