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

Adding an item to a HierarchicalDataTemplated TreeView with converter collapses tree

2 Answers 137 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 31 Jul 2012, 12:29 PM
Hi All,
    I have an issue where I am binding my ViewModel to the TreeView and using a hierarchical data template to allow a self referencing list of items.

I have got this all working fine - however, when I add a new item to the source list, it causes the itemssource on the treeview to be re-evaluated. This in turn calls the converter (which is using a linq query to find the top level of the tree and children).

I think that by calling this converter, the TreeView is losing track of which items were expanded because when the control refreshes, the TreeView is collapsed

This is my converter code (pretty simple):
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
           var item = value as Structure;
           var items = value as SturctureCollection;
 
           if (items != null)
           {
               return items.Where(i => i.ParentID == 0);
           }
           else if (item != null)
           {
               var parent = (item.Parent as StructureCollection);
 
               return parent.Where(i => i.ParentID == item.ID);
           }
 
           return null;
       }

Any idea if this is easily recitified with an option on the control? If not I assume I will need to track which items are expanded (I was thinking about just putting the IDs in a list and iterating), if so which event is best to use when the underlying itemssource is re-evaluated?

Thanks,
Charles

2 Answers, 1 is accepted

Sort by
0
Charles
Top achievements
Rank 1
answered on 31 Jul 2012, 01:25 PM
My current solution is so far to add the IDs of the items to a list on the Expanded event and remove them on Collapsed event

Then on ItemPrepared event I expand the node depending on if it's in the list of IDs or not.

Of course this code is in my view which I don't like.

Additionally, I had to turn off animation for the tree as it would fire the animations when the grid was refreshing.

Anyone else have a better suggestion?

0
Petar Mladenov
Telerik team
answered on 03 Aug 2012, 12:01 PM
Hello Charles,

 If your Converter fires this means that the ItemsSource of the RadTreeView is reset. RadTreeView does not internally preserves the state of ts RadTreeViewItems. But this can be achieved if you use ViewModels. You can introduce boolean property Expanded in your ViewModel class and bind it to the IsExpanded property of the RadTreeViewItems by StyleBingins. You can check out this help article for further reference.

Kind regards,
Petar Mladenov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
TreeView
Asked by
Charles
Top achievements
Rank 1
Answers by
Charles
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or