Expanding databound treeview after rebinding

5 posts, 0 answers
  1. paul orsillo
    paul orsillo avatar
    31 posts
    Member since:
    Jan 2010

    Posted 06 Jul 2010 Link to this post

    Hi, I have a tree that is bound to an ObservableCollection. The user can drag from the tree to a RadDataGrid. On drop the item is removed from the source collection and the collection is rebound like this. Similarly if the user deletes the row from the grid the object is inserted back into the bound collection. ItemsSource is reset like this:
    treeview.ItemsSource = null;
    treeview.ItemsSource = myCollection;

    When the ItemsSource is reset, the tree collapses as expected. But everything I have tried to re-expand it, appears to do nothing.
    My first thought was to use ExpandAll() but I find that it only works at Loaded time.

    I tried a few other thinds but my last attempt is to traverse the IntemContainers by handling the ItemContainerGenerator.StatusChanged event, finding the last selected item and setting IsExpanded on each item between the root and the selected item to true. Not only does this not work but when I inspect the IsExpanded value at runtime it is false before setting (as expected) but it is still false even after the statement  "IsExpanded = true;" is executed.

    The code I use to do this is:
                            RadTreeViewItem child = rootItem.NextItem;
                            while (child != null) {
                                child.IsExpanded = true;
                                child = child.NextItem;
                            }

    As I said, if child.IsExpanded == false before setting it. It stays false;

    I also notice that in the last iteration of the loop child.NextItem does not simply return null but throws a NullReferenceException. I suspect this has to do with virtualization. That's behavior I didn't expect.  

    Any ideas on what I am doing wrong or what the correct way is to do this?

    Thank you,
     Paul
  2. Miro Miroslavov
    Admin
    Miro Miroslavov avatar
    588 posts

    Posted 09 Jul 2010 Link to this post

    Hello paul orsillo,

    Since you are using ObservableCollections for ItemsSoureces, you don't need to rebind the RadTreeView and the RadGridView after each drag and drop operation. It will just works in that case and this will remove the need to explicitly expand the previous expanded items.
    You can also refer to this help article for Drag&Drop between RadTreeView and other list controls. Read more..
    If you encounter further problems, please ask us for support.

    Kind regards,
    Miro Miroslavov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. paul orsillo
    paul orsillo avatar
    31 posts
    Member since:
    Jan 2010

    Posted 09 Jul 2010 Link to this post

    Hi thanks for your reply.

    You are right about the binding. At one point I had the tree ItemsSource bound to a List<> and not an ObservableCollection. Then realized that was incorrect. 

    My main issue though is that the tree always collapses after an item is removed. I haven't found a way to re-expand it. It simply needs to be fully expanded, not positioned to any particular node. If I could disable collapse completely that would be ok too.
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 14 Jul 2010 Link to this post

    Hello paul orsillo,

    A possible approach towards your scenario would be to databind the IsSelected property of each RadTreeViewItem using a ContainerBinding and use an appropriate property in the business object to expand the updated collection.

    I prepared a sample project illustrating this approach. Please take a look at it and let me know if it works for you.

    Best wishes,
    Tina Stancheva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. paul orsillo
    paul orsillo avatar
    31 posts
    Member since:
    Jan 2010

    Posted 07 Dec 2010 Link to this post

    Hi Tina.

    I apologize for responding so late to your posting.  I was dragged off into another direction and just got back to this problem.

    I tried your suggestion and it works exactly as it should - just as you say. Your example was spot on. 

    Thank you so much.

     Paul 
Back to Top
DevCraft banner