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

Retrieving RadTreeViewItem using code

3 Answers 85 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Indu VS
Top achievements
Rank 1
Indu VS asked on 21 Aug 2009, 10:34 AM
I want to retrieve RadTreeViewItems from a RadTreeView. i tried the below code in a method,but it is not working.
here 'controlsTree' is the radtreeview and 'item' is the datacontext of that node.

RadTreeViewItem itemss = controlsTree.ItemContainerGenerator.ContainerFromIndex(1) as RadTreeViewItem;
or
RadTreeViewItem itemss = controlsTree.ItemContainerGenerator.ContainerFromItem(item) as RadTreeViewItem;

Is there any other method to do this?

3 Answers, 1 is accepted

Sort by
0
Tihomir Petkov
Telerik team
answered on 21 Aug 2009, 11:40 AM
Hello Indu,

The methods you are using would work only for the immediate children of the treeview. So, if you try to get a treeview item that is not at the top-most level of items using the code you provided in your post,
you will not be able to get it because the ItemContainerGenerator is hierarchly aware. Meaning that the TreeView.ItemContainerGenerator will only know direct children of the TreeView. and the TreeViewItem.ItemContainerGenerator will only knows it's own direct children.
So essentially a TreeView has as many ItemContainerGenerators as it has TreeViewItems+1. And again, each of these ItemContainerGenerators only knows about the children of the instance it belongs to. So, to be able to get the item you need, you should use the ItemContainerGenerator of the item's parent - this is either the TreeVIew itself if the item is at the root level, or another treeview item.

you may find these two links helpful:
http://silverlight.net/forums/t/65277.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator.aspx

Let me know if you have more questions.

Regards,
Tihomir Petkov
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
StrandedPirate
Top achievements
Rank 1
answered on 22 May 2010, 09:43 PM
Being new to silverlight and this wacked API I had the same issue. I finally found the RadTreeView.ContainerFromItemRecursive() method that allows you to do this. Here is some sample code for reference where I am recursively iterating over the Items collection to find one that matches a string and using the ContainerFromItemRecursive method to find the UI element that represents the item. This is for Q1 2010 version.

 

 

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)

 

{

    FindItem(FolderTreeView.Items);

}

 

 

private void FindItem(ItemCollection collection)

 

{

 

 

foreach (tbl_Path path in collection)

 

{

 

 

RadTreeViewItem item = FolderTreeView.ContainerFromItemRecursive(path) as RadTreeViewItem;

 

 

 

if (path.Name().ToLowerInvariant().StartsWith(FindTextbox.Text.ToLowerInvariant()))

 

{

item.IsSelected =

 

true;

 

 

 

break;

 

}

 

 

else if (item.HasItems)

 

{

FindItem(item.Items);

}

}

}

0
Bobi
Telerik team
answered on 26 May 2010, 02:45 PM
Hi Joey,

Thank you for the feedback. You can find some more help  here:
http://www.telerik.com/help/silverlight/radtreeview-retrieving-checked-items.html

Please let us know if you have any other questions or need some more help.

Best wishes,
Bobi
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Indu VS
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
StrandedPirate
Top achievements
Rank 1
Bobi
Telerik team
Share this question
or