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

Expanding databound treeview after rebinding

4 Answers 57 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
paul orsillo
Top achievements
Rank 1
paul orsillo asked on 06 Jul 2010, 04:08 PM
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

4 Answers, 1 is accepted

Sort by
0
Miro Miroslavov
Telerik team
answered on 09 Jul 2010, 07:59 AM
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
0
paul orsillo
Top achievements
Rank 1
answered on 09 Jul 2010, 01:42 PM
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.
0
Tina Stancheva
Telerik team
answered on 14 Jul 2010, 04:37 PM
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
0
paul orsillo
Top achievements
Rank 1
answered on 07 Dec 2010, 03:30 PM
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 
Tags
TreeView
Asked by
paul orsillo
Top achievements
Rank 1
Answers by
Miro Miroslavov
Telerik team
paul orsillo
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or