Matthew Cartwright
Top achievements
Rank 1
Matthew Cartwright
asked on 25 Nov 2009, 03:08 PM
In my application I have hierarchical, load on demand data. My view auto-refreshes every 60 seconds and I want the treeview to maintain it's expanded items, the selected item and also the scroll position.
What is the recommended approach for refreshing a tree and maintaining state?
Thanks
What is the recommended approach for refreshing a tree and maintaining state?
Thanks
3 Answers, 1 is accepted
0
Hello Matthew,
Have you considered using ContainerBindings along with your HierarchicalData. ContainerBindings allow you to bind a property of the container(RadTreeViewItem's IsSelected, CheckState, IsExpanded etc.) to a property in your model. I am attaching a sample project that demonstrates the usage of ContainerBindings. Also, in my project I refresh the data in the treeview every 5 seconds. Have a look at the example and if something is unclear or you have additional questions, let me know.
Regards,
Kiril Stanoev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Have you considered using ContainerBindings along with your HierarchicalData. ContainerBindings allow you to bind a property of the container(RadTreeViewItem's IsSelected, CheckState, IsExpanded etc.) to a property in your model. I am attaching a sample project that demonstrates the usage of ContainerBindings. Also, in my project I refresh the data in the treeview every 5 seconds. Have a look at the example and if something is unclear or you have additional questions, let me know.
Regards,
Kiril Stanoev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Matthew Cartwright
Top achievements
Rank 1
answered on 26 Nov 2009, 11:30 AM
Upon first view it looks like a really good idea, but then thinking about it this requires that my data-model is altered for specific support of the telerik treeview - which then seems like a really bad idea. Maybe extension methods would fill this gap, or inherited objects specifically for use on the treeview...hmmmm...
0
Hi Matthew,
In my opinion, using ContainerBindings is not necessarily bad idea. Actually they are really close to the ViewModel from the MVVM pattern.
Another way to achieve what you are looking is to use RadTreeView's GetItemByPath(). The main idea is the following: Every item keeps full path(string) to the root of the tree. Every time an item is selected or expanded, you add the full path of the item to some collection(ObservableCollection<string>). After you refresh, you iterate through the list of selected/expanded items and use GetItemByPath to select/expand the item:
The attached sample project demonstrates how your business objects can build full path. Have a look at it and let me know how this works for you.
Once again, I'd recommend you use the ContainerBindings approach, since if you compare the 2 projects, you will agree that the first one(with ContainerBindings) is much cleaner and easy to maintain.
Let me know which approach you favor.
Regards,
Kiril Stanoev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
In my opinion, using ContainerBindings is not necessarily bad idea. Actually they are really close to the ViewModel from the MVVM pattern.
Another way to achieve what you are looking is to use RadTreeView's GetItemByPath(). The main idea is the following: Every item keeps full path(string) to the root of the tree. Every time an item is selected or expanded, you add the full path of the item to some collection(ObservableCollection<string>). After you refresh, you iterate through the list of selected/expanded items and use GetItemByPath to select/expand the item:
// go through all selected items and make the treeview select them
foreach
(
string
itemPath
in
selectedItems.ToList())
{
if
(treeView1.GetItemByPath(itemPath,
"|"
) !=
null
)
{
treeView1.GetItemByPath(itemPath,
"|"
).IsSelected =
true
;
}
}
// go through all expanded items and make the treeview select them
foreach
(
string
itemPath
in
expandedItems.ToList())
{
if
(treeView1.GetItemByPath(itemPath,
"|"
) !=
null
)
{
treeView1.GetItemByPath(itemPath,
"|"
).IsExpanded =
true
;
}
}
The attached sample project demonstrates how your business objects can build full path. Have a look at it and let me know how this works for you.
Once again, I'd recommend you use the ContainerBindings approach, since if you compare the 2 projects, you will agree that the first one(with ContainerBindings) is much cleaner and easy to maintain.
Let me know which approach you favor.
Regards,
Kiril Stanoev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.