The tree should turn around completely, but only the first level unfolds.

ExpandRecursively2(tw_tree, true); private static void ExpandRecursively2(ItemsControl itemsControl, bool expand) { ItemContainerGenerator itemContainerGenerator = itemsControl.ItemContainerGenerator; for (int i = itemsControl.Items.Count - 1; i >= 0; --i) { TreeViewItem treeViewItem = itemsControl as TreeViewItem; if (treeViewItem != null) treeViewItem.IsExpanded = expand; // ItemsControl childControl = itemContainerGenerator.ContainerFromIndex(i) as ItemsControl; if (childControl != null) ExpandRecursively2(childControl, expand); } } 

In this strange behavior.

If you add to the menu ExpandRecursively2(tw_tree, true); and press many times.

Then each time the next deeper branch opens.

Here childControl is sometimes 0 . Because of this, the next branch does not go.

Why can it be equal to 0. What is wrong?

  ItemsControl childControl = itemContainerGenerator.ContainerFromIndex(i) as ItemsControl; 

    2 answers 2

    The answer was already in the English version of the stack -> link

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

    UPD: It should work

     private void ExpandAll(TreeViewItem items, bool expand) { foreach (TreeViewItem obj in items.Items) { if (obj.Items != null && obj.Items.Count > 0) { ExpandAll(obj, expand); } items.IsExpanded = true; } } private void btnExpandAll_Click(object sender, RoutedEventArgs e) { foreach (TreeViewItem item in this.testTreeView.Items) { ExpandAll(item, true); } } 
    • It did not change anything. All different options behave the same. - codename0082016
    • Updated the answer, check - Shakra
    • I have in items.Items) are objects of type Node (my class) - codename0082016
    • If your treeviewitem is changed, this does not change the principle of operation, if the principle changes, then you need to look at the implementation of Node . PS In wpf, usually these things are done in the ViewModel where the 'opening' and 'closing' of elements occur, and in xaml the style of the tree is set. - Shakra
    • I sometimes have childControl = 0 why can it be equal to 0? - codename0082016

    Solved. https://stackoverflow.com/questions/1902630/how-to-expand-all-nodes-of-a-wpf-treeview-in-code-behind#1902630

      <TreeView.ItemContainerStyle> <Style TargetType="TreeViewItem"> <Setter Property="TreeViewItem.IsExpanded" Value="True"/> </Style> </TreeView.ItemContainerStyle>