Adding an item to a HierarchicalDataTemplated TreeView with converter collapses tree

3 posts, 0 answers
  1. Charles
    Charles avatar
    15 posts
    Member since:
    Dec 2010

    Posted 31 Jul 2012 Link to this post

    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?

  2. Charles
    Charles avatar
    15 posts
    Member since:
    Dec 2010

    Posted 31 Jul 2012 Link to this post

    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?

  3. Petar Mladenov
    Petar Mladenov avatar
    2991 posts

    Posted 03 Aug 2012 Link to this post

    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.

Back to Top