I'm using RadControls for WPF (version 2012.1.326.40), and my question is about the RadTreeView.
I want to select by code a specific item even if all nodes of the treeview has not been generated.
My treeview is using the virtualization mode, so the items are not generated until the node is expanded.
How can I select an item ? If I explore the nodes, I have only the generated nodes.
And I can't use the BringPathIntoView method, because my nodes have different types so it's impossible for me to use the property TextSearch.TextPath.
Do you have any solutions ?
6 Answers, 1 is accepted
If you just want to select particular item you could bind the IsSelected property of the TreeViewItem to property in your view model. All items in the view model are present all the time, so selecting particular item should not be a problem.
Additionally, if you want to display the just selected item you could do one of the following:
1) Use the BringIntoView method after the Loaded event of the TreeView;
2) Introduce a common property among all your different object used in the TreeView.
the Telerik team
actually I don't have a view model for each TreeViewItem, I bound directly the model to the view, that's why I have no way to add a common property (IsSelected or whatever).
The only way is to provide a view model for my TreeViewItem ?
The best way of controlling your view is by using MVVM pattern. This means you have a ViewModel class that posses some properties which are going to be bound to the view and control it in some aspects.
However if you want to spare the cost of creating such and objects (which I do not personally recommend you to do), you could manipulate the visual control itself. In your case, this means you should manually instruct the TreeView what to do. You could expand containers via code behind starting from the top level. When you expand an item you should wait until the child item to be created and added to the visual tree. This means you should listen to ItemPrepared event of the TreeView and check if the next item that needs to be expand is created. If so, you could continue expanding the rest of items down the path. Also you may need to call bring into (most probably you will use the BringIndexIntoView) view to the respective child of the just expanded item (in case your parent item has many children and the children of interest is not in the view port).
As you can see, this implementation is quite complex and there are many tricky parts related to asynchronous container creation.
the Telerik team
mmmh, you're right, it seems complex...
I'm trying the MVVM pattern, and I will see.
Thanks for your reply.
I am working on a similar problem where I am loading the nodes at all indentation levels virtually, I am using the MVVM pattern and now I need to bring a particular item that I know has been created and I have in my possession into view. I am not seeing a solution whereby I can call BringItemIntoView (either directly or indirectly) from the view model without violating the MVVM pattern. If there is an alternate way to force an item into the displayed portion of the view (in this case RadTreeView) from the view model I would be interested in that approach as well. I am not committed to BringItemIntoView it just seems the only way to cause a particular item to be programmatically forced into the viewed region of the RadTreeView. Could you please provide an example of how such a solution can be implemented.
If you want to programmatically bring an item into view from a call initiated by your view model you could chose one of the following approached:
1) Use a messenger to fire a message to other part of the application that is not a view model and has a reference to the TreeView. Thus registering from the message of interest you could call the TreeView bring into view functionality.
2) Use a service. Create an interface with one method "BringIntoView". In your view model declare a member of this type and call its method when required. Also use dependency injection (for example constructor injection) to pass a concrete implementation of this interface. The concrete implementation may have a reference to the TreeView (i.e. a reference to a visual element) and could call the bring into view functionality. But the view model itself will not contain a reference to the visual because is working only with the interface.
You could check in Google how to open a MessageBox from inside your view model, it is the same approach.
Hope this helps.
the Telerik team