TreeViewItem.ExpandSubtree Method ()

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Expands the TreeViewItem control and all its child TreeViewItem elements.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

Public Sub ExpandSubtree

The following examples show how to find the selected TreeViewItem and expand it and all of it child items. The following XAML creates a TreeView and populates it with some data.

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

    <!-- 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}"/>

  <TreeView Name="treeView1"
            ItemsSource="{Binding Source={StaticResource myCompany}}" 
            ItemTemplate="{StaticResource EmployeeTemplate}"/>
  <Button Name="expandSelect" 
            Content="Expand _Selected Item"


The following code traverses the TreeView to find the selected TreeViewItem and then calls ExpandSubtree to display all child items of the selected TreeViewItem.


The GetTreeViewItem method only works for TreeViewItem controls that are not virtualized. To learn how to find a TreeViewItem that may be virtualized, see How to: Find a TreeViewItem in a TreeView.

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
    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
    End If
    Return tvi
End Function

.NET Framework
Available since 4.0
Return to top