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

Programmatically set SelectedItem and Expand it

5 Answers 1219 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 15 Nov 2011, 07:30 PM
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?

5 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 18 Nov 2011, 03:39 PM
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 >>

0
Yu
Top achievements
Rank 1
answered on 20 Dec 2011, 08:48 PM
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,
0
Tina Stancheva
Telerik team
answered on 23 Dec 2011, 05:29 PM
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 >>

0
hosien
Top achievements
Rank 1
answered on 11 Dec 2016, 09:54 AM

hi 

Im need a Selecte frist Item in RadTreeView .

help me

0
Martin Ivanov
Telerik team
answered on 14 Dec 2016, 11:38 AM
Hello hosien,

You can get the first item of the treeview control and set it as a value of the SelectedItem property.
treeviewControl.SelectedItem = treeviewControl.Items[0];

Regards,
Martin
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
TreeView
Asked by
Josh
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Yu
Top achievements
Rank 1
hosien
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or