I am using the RadTreeView control and I want to Select a node in the code behind. I have looked at the previous post on this topic and none of the suggestions worked. Here is the code I am using to get the item:
When I am through I want my tree to look like this:
· Doc 1(SELECTED)
My item is null after the call to ContainerFromItemRecursive. I already have the tree expanding to the folder level.
5 Answers, 1 is accepted
I assume the node you are trying to select is not visible(its parent is not expanded).
If this is the case, the you have to make the node visible first. To do that, you can use the ExpandItemByPath method of RadTreeView. The ExpandItemByPath takes 2 arguments: the full path to the node and a separator.
Assume the following ViewModel:
Every Person, Department and Organization has a name(represented by a string property Name).
Imagine you are looking for a person called "John", who is in the "Silverlight Department", which is part of "Microsoft". The most basic way this can be done is via 3 foreach loops:
As you can see, as soon as there is a match between the employee we are looking for and the current employee, the method also creates a full path to this employee by taking the name of the organization, department and employee separated by "|".
Once constructed, the full path can be used to select the container of the item:
I am attaching my sample project for further reference. Have a look at it and if you have additional questions, let me know.
All the best,
the Telerik team
1) How do I expand to the "Scott Guthrie" node without user interaction? In other words at what point in the page loading lifecycle is it safe to call the code to select the item. I have tried after the InitializeComponent() call in the page constructor, in the call back method of the page Loaded event, and the call back method of the tree loaded event. Each time the treeView.ContainerFromItemRecursive(emp).IsSelected = true; line of code is called it throw an exception that the ContainerFromItemRecursive call is null.
2) If I don't want to have the Name property be the property for building the path how do I do that? I have tried setting this
:TextSearch.TextPath="Path" on treeview control. I want to use a property called "Path".
1) The reason why the item you want to expand is null is because the treeview items have not been generated yet. That is why you will need to use a Dispatcher. Simply attach to the MainPage's Loaded event and execute the same logic using a dispatcher.
2) In the override ToString method of each business object (Employee, Department and Organization) you will have to return not the Name property, as it is now, but you Path property;
Also, you will have to modify your FindEmployee method a little bit.
Let me know if you have additional questions.
the Telerik team
Problematic is the word because, when I am trying to expand down multiple levels, a NULL pointer can be thrown. In my testing I found a good way to make this happen very quickly but I'm building a whole project for this to submit to Teleik, I'm late on this project as it is. So here are my notes for the next person to run into this problem: