This documentation is archived and is not being maintained.

TreeViewItem.ExpandSubtree Method

Expands the TreeViewItem control and all its child TreeViewItem elements.

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

public void 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 void expandSelected_Click(object sender, RoutedEventArgs e)
    if (treeView1.SelectedItem == null)

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

    if (tvi != null)

// 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;

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.