ExpandAll/CollapseAll recursively?

7 posts, 0 answers
  1. Beau
    Beau avatar
    5 posts
    Member since:
    Aug 2012

    Posted 04 Apr 2012 Link to this post

    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?
  2. AKROS
    AKROS avatar
    68 posts
    Member since:
    Oct 2011

    Posted 05 Apr 2012 Link to this post

    Hello,

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

    Greetings
  3. DevCraft banner
  4. Beau
    Beau avatar
    5 posts
    Member since:
    Aug 2012

    Posted 09 Apr 2012 Link to this post

    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.
  5. Beau
    Beau avatar
    5 posts
    Member since:
    Aug 2012

    Posted 19 Apr 2012 Link to this post

    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 :)
  6. Jonathan
    Jonathan avatar
    8 posts
    Member since:
    Jun 2009

    Posted 01 May 2012 Link to this post

    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();
    }
    }
  7. Lior
    Lior avatar
    28 posts
    Member since:
    Aug 2012

    Posted 15 Jan 2013 Link to this post

    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.
  8. Gopala
    Gopala avatar
    28 posts
    Member since:
    Jul 2012

    Posted 27 Aug 2013 Link to this post

    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);
        }
Back to Top
DevCraft banner