Accessing item right after adding

2 posts, 1 answers
  1. Andreas
    Andreas avatar
    14 posts
    Member since:
    Feb 2011

    Posted 18 Apr 2012 Link to this post


    I have a problem regarding the treeview:
    When i add an item to the treeview like this
    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?

  2. Answer
    Tina Stancheva
    Tina Stancheva avatar
    3299 posts

    Posted 18 Apr 2012 Link to this post

    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 >>

Back to Top