Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollapseAll items of a treeview within Wpf application

I have a WPF application , in which i have a treeview control .

I'd like to collapse all the items by clicking into a button. I tried this:

 private void buttonParam_Click(object sender, RoutedEventArgs e)
        {
//handling
    this.arborescence.IsExpanded = false;
        }

but it didn't worked. What are the reasons of this error? How can i change my snippet to do this task?

like image 745
Lamloumi Afif Avatar asked Dec 02 '22 21:12

Lamloumi Afif


1 Answers

I think the best way to do this is by looping through all nodes and collapse them 1 by 1.

private void cmdCollapse_Click(object sender, RoutedEventArgs e)
{
    foreach (TreeViewItem item in treeview.Items)
        CollpaseTreeviewItems(item);
}

void CollapseTreeviewItems(TreeViewItem Item)
{
    Item.IsExpanded = false;

    foreach (TreeViewItem item in Item.Items)
    {
        item.IsExpanded = false;

        if (item.HasItems)
            CollapseTreeviewItems(item);
    }
}
like image 82
ValarmorghulisHQ Avatar answered Jan 02 '23 11:01

ValarmorghulisHQ