WPF TreeView: Where is the ExpandAll() method

How can I expand all TreeView nodes in WPF? In WinForms there was a ExpandAll() method which does this.


This might help

<TreeView>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="True" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

with XAML Treeview style you must have a property setter like that what wrote above :

In Cs file, write methods like this, in my sample i used a button and my treeview's name is myTV :

private void ExpandAll(ItemsControl items, bool expand)
    {
        foreach (object obj in items.Items)
        {
            ItemsControl childControl = items.ItemContainerGenerator.ContainerFromItem(obj) as ItemsControl;
            if (childControl != null)
            {
                ExpandAll(childControl, expand);
            }
            TreeViewItem item = childControl as TreeViewItem;
            if (item != null)
                item.IsExpanded = true;
        }
    }


    private void btnExpandAll_Click(object sender, RoutedEventArgs e)
    {

        foreach (object item in this.myTV.Items)
        {
            TreeViewItem treeItem = this.myTV.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
            if (treeItem != null)
                ExpandAll(treeItem, true);
            treeItem.IsExpanded = true;
        }
    }

hope it could help you.


The WPF TreeView class does not have an ExpandAll method. Thus you'd have to loop through the nodes and set their IsExpanded properties to true.