This is a migrated thread and some comments may be shown as answers.
ExpandAll/CollapseAll recursively?
6 Answers 96 Views
This is a migrated thread and some comments may be shown as answers.
Beau
Top achievements
Rank 1
Beau asked on 05 Apr 2012, 03:19 AM
Hi,

I want to collapse/expand all the nodes below the selected node recursively. The treeview seems to have a ExpandAll(Object) function, but this is missing in the treeListView.

I can only see methods to collapse/expand everything, or methods to collapse/expand a specific element, but nothing that allows me to collapse all the elements beneath a specified element.

How would I accomplish this?

6 Answers, 1 is accepted

Sort by
0
AKROS
Top achievements
Rank 1
answered on 05 Apr 2012, 11:25 AM
Hello,

Did you try with the method ExpandHierarchyItem(object o)?

Greetings
0
Beau
Top achievements
Rank 1
answered on 10 Apr 2012, 02:02 AM
Hi,

ExpandHierarchyItem(object o) expands only that item.

I would also like to expand the children of that item, and any children they themselves might have. It's possible for me to write a method to do this, via accessing the items children and calling ExpandHierarchyItem on each children, but I was hoping the control would support this functionality by itself.
0
Beau
Top achievements
Rank 1
answered on 20 Apr 2012, 01:36 AM
So in order to accomplish this I created some extensions to the treeListView class. I'm not completely happy as you have to throw in a delegate to get the children. But at least this sits in more of a DRY/ code reuse zone than the solutions I've seen (requiring a control component to have intimate knowledge of it's data, to perform a function that just displays it is rediculous in my opinion)

public static class TreeViewExtensions {
    public static void CollapseAll(this RadTreeListView radTreeList, object o, Func<object, IEnumerable> getChildren) {
        //First Collapse the individual node
        radTreeList.CollapseHierarchyItem(o);
        //Then get all the children
        IEnumerable children = getChildren(o);
          
        //Loop through and call recursively
        foreach (var child in children) {
            radTreeList.CollapseAll(child, getChildren);
        }
    }
    public static void ExpandAll(this RadTreeListView radTreeList, object o, Func<object, IEnumerable> getChildren) {
        //First Expand the individual node
        radTreeList.ExpandHierarchyItem(o);
        //Then get all the children
        IEnumerable children = getChildren(o);
        //Loop through and call recursively
        foreach (var child in children) {
            radTreeList.ExpandAll(child, getChildren);
        }
    }
}

This of course requires you to pass in a delegate function, that will take a object and return it's children as an IEnumerable list. At least this allows you to have the ExpandAll(object, delegate) format. If you declare the delegate in a source file you can away with one line to expand/collapse all the children.

Enjoy :)
0
Jonathan
Top achievements
Rank 1
answered on 01 May 2012, 07:55 AM

The latest version has an ExpandAllHierarchyItems function. Ive written a simple MVVM compatible trigger to expand all items whenever the datasource changes.

using System;
using System.Windows.Interactivity;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
/// <summary>
/// When attached to a treelistview, this behaviour expands all the hierarchy items in the tree when its
/// itemssource has changed
/// </summary>
public class ExpandAllTreeListViewItemsOnLoad : Behavior<RadTreeListView>
{
/// <summary>
/// Triggers calls when attached.
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.DataLoaded += new System.EventHandler<System.EventArgs>(AssociatedObject_DataLoaded);
}
void AssociatedObject_DataLoaded(object sender, System.EventArgs e)
{
this.AssociatedObject.DataLoaded -= new System.EventHandler<System.EventArgs>(AssociatedObject_DataLoaded);
this.AssociatedObject.ExpandAllHierarchyItems();
this.AssociatedObject.DataLoading += new EventHandler<GridViewDataLoadingEventArgs>(AssociatedObject_DataLoading);
}
void AssociatedObject_DataLoading(object sender, System.EventArgs e)
{
this.AssociatedObject.DataLoaded += new System.EventHandler<System.EventArgs>(AssociatedObject_DataLoaded);
}
/// <summary>
/// Triggers calls when detached.
/// </summary>
protected override void OnDetaching()
{
base.OnDetaching();
}
}
0
Lior
Top achievements
Rank 1
answered on 16 Jan 2013, 02:20 AM
Hi Jonathan,

Thank you very much, this is what we need, this works.

another workaround like style seletor, dataloaded, rowloaded events, it  against mvvm. but  this is true in MVVM workaround, thanks very much.
0
Gopala
Top achievements
Rank 1
answered on 27 Aug 2013, 10:56 PM
Hi,
I am working on something similar in my SILVERLIGHT and I have below method. It works good for few hierarchy nodes but for large number of nodes it is bad. Any help is appreciated. I am using 2010 Q4 0924 version. Problem is we cannot update to latest telerik version

private void ExpandNode(ReportNode pNode)
    {
      if (pNode is ReportLeafNode) return;
      DetailTreeView.ExpandHierarchyItem(pNode);
      foreach (var node in pNode.Children)
        ExpandNode(node);
    }
Asked by
Beau
Top achievements
Rank 1
Answers by
AKROS
Top achievements
Rank 1
Beau
Top achievements
Rank 1
Jonathan
Top achievements
Rank 1
Lior
Top achievements
Rank 1
Gopala
Top achievements
Rank 1
Share this question
or