This is a migrated thread and some comments may be shown as answers.

How to Expand TreeView nodes individually

4 Answers 226 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
om
Top achievements
Rank 1
om asked on 07 May 2009, 10:42 PM
Hi,
 I have a scenario where I have a tree view control to which I am providing ItemSource from code behind and my collection for Item source comes from a service asynchrously.

I have almost 10 root elements added to this treeview and each root level element has two more levels.
What I want to do is that I only want to expand root elements but not their children e.g.

- Root1
   +Child
- Root2
   +Child
- Root3
   +Child

I tried following code, but didn't work. It returns me null item:

 

 

 

if (_radTree != null && _Top != null)

 

 _radTree.ItemsSource = _Top;

 

foreach (var item in _radTree.Items)

 

{


    TreeViewItem
tviItem = _radTree.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;   
    tviItem.IsExpanded =
true;

 

}

_radTree.ItemContainerGenerator.ContainerFromItem(item)    returns null I have tried ContainerFromIndex(index) as well, which also returns true.

I can not use this function in TreeView's Loaded event because, tree is empty at the time of load since my ItemSource is comming from webservice.

I have noticed there is another function but couldn't manage to use it, which is "ExpandItemByPath"

Is there any solution to my problem?

Thanks in Advance

 

 

 

4 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 08 May 2009, 07:00 AM
Hi om,

You can use the ItemPrepared event to get notifies when the containers are created. In your case this will always give you the root items and their children when expanded. This is why you can use the LEvel property to check whether it is a root item that you are trying to expand:

    treeView.ItemPrepared += new EventHandler<RadTreeViewItemPreparedEventArgs>(treeView_ItemPrepared)  
}  
 
void treeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)  
{  
    var treeViewItem = e.PreparedItem;  
    if (treeViewItem.Level == 0)  
    {  
        treeViewItem.IsExpanded = true;  
    }  
}  
 

Hopefully this is what you need,

Greetings,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
om
Top achievements
Rank 1
answered on 08 May 2009, 01:14 PM
Thanks alot Miroslav, it worked !!!
0
Jie
Top achievements
Rank 1
answered on 08 Jun 2009, 11:28 PM
I am attempting to create very similar functionality but...

The Level property for a RadViewTreeItem does not work for me.  The Level property always returns "0", no matter which item I look at in the tree, even if the item is 1,2,3 levels deep in the tree.  Could this because I am programmatically loading the items on demand in the treeview.

The IsRootItem and RootItem properties also do not seem to work, in that IsRootItem always returns "true" and RootItem returns the same RadViewTreeItem.  (e.g. Item1.RootItem == Item1)
0
Bobi
Telerik team
answered on 10 Jun 2009, 02:39 PM
Hi Jie,

Please find attached a sample project that demonstrates how to use RadTreeView with load on demand and how to get the current RadTreeViewItem Level.

I hope that this will help you.

Kind regards,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
TreeView
Asked by
om
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
om
Top achievements
Rank 1
Jie
Top achievements
Rank 1
Bobi
Telerik team
Share this question
or