다음을 통해 공유


TreeViewItem.ExpandSubtree 메서드

정의

TreeViewItem 컨트롤 및 해당하는 모든 자식 TreeViewItem 요소를 확장합니다.

public:
 void ExpandSubtree();
public void ExpandSubtree ();
member this.ExpandSubtree : unit -> unit
Public Sub ExpandSubtree ()

예제

다음 예제에서는 선택한 TreeViewItem 항목 및 모든 자식 항목을 찾아서 확장하는 방법을 보여 줍니다. 다음 XAML은 a를 TreeView 만들고 일부 데이터로 채웁니다.

<StackPanel>
  <StackPanel.Resources>
    <XmlDataProvider x:Key="myCompany" XPath="Company/Employee">
      <x:XData>
        <Company xmlns="">
          <Employee Name="Don Hall">
            <Employee Name="Alice Ciccu">
              <Employee Name="David Pelton">
                <Employee Name="Vivian Atlas"/>
              </Employee>
              <Employee Name="Jeff Price">
                <Employee Name="Kari Hensien"/>
              </Employee>
              <Employee Name="Andy Jacobs"/>
            </Employee>
            <Employee Name="Bill Malone">
              <Employee Name="Maurice Taylor">
                <Employee Name="Sunil Uppal">
                  <Employee Name="Qiang Wang"/>
                </Employee>
              </Employee>
            </Employee>
          </Employee>
        </Company>
      </x:XData>
    </XmlDataProvider>

    <!-- Bind the HierarchicalDataTemplate.ItemsSource property to the employees under
         each Employee element. -->
    <HierarchicalDataTemplate x:Key="EmployeeTemplate" 
                              ItemsSource="{Binding XPath=Employee}">
      <TextBlock Text="{Binding XPath=@Name}"/>
    </HierarchicalDataTemplate>

  </StackPanel.Resources>
  <TreeView Name="treeView1"
            ItemsSource="{Binding Source={StaticResource myCompany}}" 
            ItemTemplate="{StaticResource EmployeeTemplate}"/>
  <Button Name="expandSelect" 
            Margin="5,0,0,0"
            Content="Expand _Selected Item"
            Click="expandSelected_Click"/>

</StackPanel>

다음 코드는 선택한 TreeViewItem 항목을 찾은 다음, 선택한 모든 자식 항목을 TreeViewItem표시하는 호출 ExpandSubtree 을 트래버스 TreeView 합니다.

참고

이 메서드는 GetTreeViewItem 가상화되지 않은 컨트롤에 TreeViewItem 대해서만 작동합니다. 가상화될 수 있는 TreeViewItem 항목을 찾는 방법을 알아보려면 방법: TreeView에서 TreeViewItem 찾기를 참조하세요.

private void expandSelected_Click(object sender, RoutedEventArgs e)
{
    if (treeView1.SelectedItem == null)
    {
        return;
    }

    TreeViewItem tvi = GetTreeViewItem(treeView1, treeView1.SelectedItem);

    if (tvi != null)
    {
        tvi.ExpandSubtree();
    }
}

// Traverse the TreeView to find the TreeViewItem 
// that corresponds to the selected item.
private TreeViewItem GetTreeViewItem(ItemsControl parent, object item)
{
    // Check whether the selected item is a direct child of 
    // the parent ItemsControl.
    TreeViewItem tvi =
        parent.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

    if (tvi == null)
    {
        // The selected item is not a child of parent, so check
        // the child items of parent.
        foreach (object child in parent.Items)
        {
            TreeViewItem childItem = 
                parent.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem;

            if (childItem != null)
            {
                // Check the next level for the appropriate item.
                tvi = GetTreeViewItem(childItem, item);
            }
        }
    }
    return tvi;
}
Private Sub expandSelected_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    If treeView1.SelectedItem Is Nothing Then
        Exit Sub
    End If

    Dim tvi As TreeViewItem =
        GetTreeViewItem(treeView1, treeView1.SelectedItem)

    If tvi IsNot Nothing Then
        tvi.ExpandSubtree()
    End If
End Sub

' Traverse the TreeView to find the TreeViewItem 
' that corresponds to the selected item. 
Private Function GetTreeViewItem(ByVal parent As ItemsControl,
                                 ByVal item As Object) As TreeViewItem

    ' Check whether the selected item is a direct child of 
    ' the parent ItemsControl. 
    Dim tvi As TreeViewItem =
        TryCast(parent.ItemContainerGenerator.ContainerFromItem(item), TreeViewItem)

    If tvi Is Nothing Then
        ' The selected item is not a child of parent, so check 
        ' the child items of parent. 
        For Each child As Object In parent.Items
            Dim childItem As TreeViewItem =
                TryCast(parent.ItemContainerGenerator.ContainerFromItem(child), TreeViewItem)

            If childItem IsNot Nothing Then
                ' Check the next level for the appropriate item. 
                tvi = GetTreeViewItem(childItem, item)
            End If
        Next
    End If
    Return tvi
End Function

적용 대상