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

[Solved] Select node added to databound collection

3 Answers 92 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Adam
Top achievements
Rank 1
Adam asked on 03 Aug 2009, 06:28 PM
I have a treeview bound to an observablecollection using a hierarchical data template. When I add a new node to the collection the treeview updates itself correctly, but what would be the best way to select the new node in the treeview when this occurs? Is there a way to accomplish this with databinding?

Thanks,

Adam

3 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 03 Aug 2009, 06:42 PM
Hi Adam,

Yes, there is a way to accomplish this using bindings. You can use the ContainerBindings. Container bindings are given in a collection attached to the DataTemplate of the TreeViewItem (normally coming from ItemTemplate or ItemTemplateSelector) and they bind the container to the ViewModel (as opposed to HeaderTemplate to ViewModel).

The last post of this thread has more details on implementing the ContainerBindings:

http://www.telerik.com/community/forums/silverlight/treeview/radtreeview-selecteditem-binding.aspx

Generally though you may have the same items bound in two or more ItemsControls. Then binding the IsSelected property may not be enough. Then you can register for the ItemPrepared event of the TreeView and select all the items that you recognize as new. What you regard as "new" item may vary.

Sincerely yours,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Adam
Top achievements
Rank 1
answered on 03 Aug 2009, 07:02 PM
Thanks this helps to select the new item but is there a way to expand the tree automatically to show the selected item if it has been added under a collapsed node?
0
Miroslav
Telerik team
answered on 03 Aug 2009, 07:26 PM
Hi Adam,

If you have a reference to the item where you have added the new item, then set the IsExpadned property to true.

Another option is to search for the container using the ContainerFromItemRecursive method of the TreeView and then expand the TreeViewItem.

A third option will be to bind the IsExpadned property in a similar way (using ContainerBindings) and set it on the ViewModel.

You can choose the case that suits your scenario best.

Best wishes,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
TreeView
Asked by
Adam
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
Adam
Top achievements
Rank 1
Share this question
or