Programmatically set SelectedItem and Expand it

4 posts, 0 answers
  1. Josh
    Josh avatar
    6 posts
    Member since:
    Jun 2009

    Posted 15 Nov 2011 Link to this post

    I need to programmatically select an item in the tree view and expand it.  I can select it by setting the SelectedItem = (some object in my collection on my viewmodel).  How can I expand the SelectedItem?  I assume I need to get to the RadTreeViewItem.IsExpanded for the SelectedItem, but how do I get to the RadTreeViewItem that is selected?
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 18 Nov 2011 Link to this post

    Hi Josh,

    Please have a look at this blog post and let me know if it helps you implement your scenario or if I'm missing something.

    Kind regards,
    Tina Stancheva
    the Telerik team

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

  3. DevCraft banner
  4. Yu
    Yu avatar
    1 posts
    Member since:
    Dec 2011

    Posted 20 Dec 2011 Link to this post

    Hi Tina,
    BringPathIntoView() works in this scenario. what if i have SelectedItem set in VM and i only want to highlight the parent node that contains the specific child node?
    Why Selected event is not firing when load the RadTreeView with SelectedItem set?

    Thanks,
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 23 Dec 2011 Link to this post

    Hello Yu,

    I am not sure I understand you first requirement - highlight the parent node that contains the specific child node. Can you please elaborate more on it? Is the node that you're bringing into view also the selected node? Or you have a selected item and you need to bring into view another item?

    If your scenario is to have a selected item and then bring another item into view without selecting it but highlighting its parent item, then you can follow the approach in the blog post but without selected the item that is brought into view. Basically in the blog post solution you need to modify the BringItem method like so:
    private void BringItem(object sender, RoutedEventArgs e)
    {
        BusinessItem item = this.sampleVM.GetItemByName(this.textBox.Text);
        if (item != null)
        {
            //item.IsSelected = true;
            string path = item.GetPath();
            myTreeView.BringPathIntoView(path);
            myTreeView.ContainerFromItemRecursive(item.Parent).Background = new SolidColorBrush(Colors.Orange);
        }
    }
    Please note that the item that will be brought into view isn't selected - its IsSelected property isn't changed and that we use the RadTreeView.ContainerFromItemRecursive method to get the RadTreeViewItem container of the parent item to change its background.

    Also, when you set the SelectedItem property when loading the RadTreeView control the SelectionChanged and PreviewSelectionChanged events are fired so if you want to handle the selection or implement a custom logic based on it, these are the events that you need to handle.

    I hope this information will help you. Please let us know if we can further assist you.

    Greetings,
    Tina Stancheva
    the Telerik team

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

Back to Top