Hi Telerik,
Hope all are well and keeping up the good work with your controls! :) I've encountered a small problem with the Method -: BringPathIntoView
I am currently writing a tree filter routine and I use this method to bring the selected item into View. However there is an unintentional bug in this routine.
My Path looks something like this -: Control|Europe|Amsterdam|AM|User1
Which has kinda been working. However in my Tree I have the following Heierarchy.
Control->Europe|Amsterdam|Billy
->Europe|Amsterdam|Brian
Office ->Asia
My search for Billy succeeds fine - however my search for Brian fails. I can tell why it fails because there are duplicate 'Europe' enteries and if the routine fails to find an element it doesn't bother to carry on checking. The other side of the argument is - should we have two europes? In this case I can actually get the name changed - phew/ This raises two questions -:
1. How can I work around this problem?
2. Can the treeview control be updated so that it doesn't give up on the first depth search?
regards
Richard
7 Answers, 1 is accepted
When searching the TreeViewItem-s with databound RadTreeView you should keep in mind that the control takes the string that is passed through the path parameter and searches through the RadTreeView Items collection to find the first match. Unfortunately the matching mechanism searches the items Headers that are strings and that's why it always returns the first match and not the unique one.
To create your own intelligent search method you might use the following workaround:
Let's say that your business object collection contains not only strings but unique Key associated with each TreeViewItem:
class Items
{
string Key { get; set; }
string Name { get; set; }
Items[] Children { get; set; }
}
You can set the TextPath of the TreeView to this "Key" by attaching this to the TreeView:
telerik:TextSearch.TextPath="Key"
Then you can use the GetItemByPath property to expand to a given item.
var treeItem = treeView.GetItemByPath("Key1/Key2/key3", "/");
So instead of Control|Europe|Amsterdam|AM|User1 you will have Key1|Key2|Key3|Key4|Key5.
You can also use treeView.ExpandItemByPath(...) if you need to expand to that item and item.BringIntoView() if you want to make sure that the item is visible for the user.
I hope this information helps.
Regards,
Evgenia
Telerik
Hi Evgina,
Many thanks for you reply. I am very grateful. :) I am interested in your response because I had a similar idea however I wasn't sure. The problem is I am dealing with legacy code. I am not allowed to make changes to this legacy code. I have created another control that takes user input to filter the tree. The problem is that the RadTreeViews I am using are populated in two different ways!!!
AARRGGHH!
1. Adds RadTreeViewItems manually from a data feed. It sets the DataContext of each RadTreeViewItem to be that of the DataSource item it represents.
2. The second Tree binds items via MVVM using the ItemsSource property.
I've got a solution that pretty much works the problem is I don't think that search will work on both trees....
From your last sentence we are not sure whether you have hit particular issue or you have concerns that something might not work. Could you please elaborate a bit more ?
Generally, we strongly encourage you to avoid creating manually RadTreeViewItems and assingning them DataContexts. You loose the ability to virtualize the treeview and this will be slow.
Regards,
Petar Mladenov
Telerik
Hi Petar,
Its ok - its a legacy code base and its been worked on by many developers. The fact that you find the last sentence confusing is just a reflection of this! :)
I've not been able to get this working just yet but I have something that seems acceptable thus far.
regards
Richard
Hello Evgenia and Peta,
I have a question regarding the BringIntoView code. If I turn on Virtualisation will the call still work?
regards
Richard
BringPathIntoView method of RadTreeView is especially designed to work in virtualized and yet-not-loaded trees. It will internally expand items while iterating the given path parameter and this will force the generation of containers (RadTreeViewItems). Also, if it is invoked before the control is loaded, the method internally listens for the Loaded event ensuring the method will be successful on load.
Regards,
Petar Mladenov
Telerik
Hi,
Just want to thank everyone for their responses - I have managed to successfully implement the code changes and have made the application a LOT more functional. Was a tricky task!
regards
Richard