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

Accessing item right after adding

1 Answer 46 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Andreas
Top achievements
Rank 1
Andreas asked on 18 Apr 2012, 09:31 AM
Hey!

I have a problem regarding the treeview:
When i add an item to the treeview like this
this.SelectedItem.Children.Add(itemToAdd);
this.SelectedItem = itemToAdd;


i cannot access the added item right after:
//Open element after child added
var item = this.view.TreeView.SelectedContainer as RadTreeViewItem
if (item != null)
{
item.IsExpanded = true;
}

But i need the added item right after not only for expanding purposes.
how can i get the item immediately after adding?

thanks
andi

1 Answer, 1 is accepted

Sort by
0
Accepted
Tina Stancheva
Telerik team
answered on 18 Apr 2012, 01:38 PM
Hello Andreas,

Looking at your code snippets I can see that you're adding the new item in a business collection of items. However, in order to get the SelectedContainer of the RadTreeView control, the RadTreeViewItem container, that wraps the newly added and selected business item, should be generated.

But the RadTreeViewItem containers of the business items in the RadTreeView control are only generated after the business items is visualized - basically this means that after adding a new item, you need to make sure its ParentItem is expanded to display it. In that case, the RadTreeView ItemPrepared event will be fired for the newly added item and you can handle it to implement your custom logic.

Still, in databinding scenarios we recommend binding all properties of the RadTreeViewItem containers, that you'll need to modify - IsExpanded, DefaultImageSource, etc. This way you won't have to wait for the containers to generate and you will only work with the data items in your view models. And as soon as the RadTreeViewItem containers are generated, their property bindings will be applied accordingly.

All the best,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
TreeView
Asked by
Andreas
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or