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

How to get RadTreeViewItem by tag

3 Answers 119 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Srini
Top achievements
Rank 1
Srini asked on 02 Sep 2009, 09:33 AM

I have RadTreeView like this

<telerik:RadTreeView x:Name="treeView" IsLineEnabled="True" SelectionMode="Multiple" IsEditable="True">
                    <telerik:RadTreeViewItem x:Name="rtvSearch" Header="Search" Tag="SEARCHINFO"/>
                    <telerik:RadTreeViewItem x:Name="rtvOpenNewAccount" Header="Open New Account" Tag="CONTACTNO" />

                    <telerik:RadTreeViewItem x:Name="rtvManageAccount" Header="Manage Account" Tag="MANAGEACCOUNT" >
                        <telerik:RadTreeViewItem x:Name="rtvProfile" Header="Profile" Tag="PROFILE" />
                        <telerik:RadTreeViewItem x:Name="rtvName" Header="Name" Tag="NAME" />
                        <telerik:RadTreeViewItem x:Name="rtvAddress" Header="Address" Tag="ADDRESS"/>
                    </telerik:RadTreeViewItem>

                    <telerik:RadTreeViewItem x:Name="rtvMakePayment" Header="Make Payment" Tag="PAYMENT"  />
                    <telerik:RadTreeViewItem x:Name="rtvVehicle" Header="Vehicle/Transponder" Tag="VEHICLETRANSPONDER" >
<telerik:RadTreeView>

In CodeBehind:

I have a Method 

        public void SelectNavigationContent(string tag)
        {
            try
            {
                for (int i = 0; i < treeView.Items.Count; i++)
                {
                    RadTreeViewItem item = treeView.ItemContainerGenerator.ContainerFromItem(treeView.Items[i]) as RadTreeViewItem;
                    if (item != null)
                    {
                        if (item.Tag != null)
                        {
                            if (item.Tag.ToString() == tag)
                                item.IsSelected = true;
                            else
                                item.IsSelected = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

This Method is to Select TreeViewItem According to the tab selection...
Now the problem is, it is jumping out of the item and not going in to the Tag  PROFILE,NAME and ADDRESS since it is a child to one of the Item...

3 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 02 Sep 2009, 11:48 AM
Hello Srini,

Yes, the Items property only contains the immediate children of an items control,
you have to get their items and containers recursively.

Kind regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Srini
Top achievements
Rank 1
answered on 02 Sep 2009, 12:33 PM
Thanks for your reply..

Can u tell me how to get the child root items of a RadTreeViewItem...
0
Accepted
Miroslav
Telerik team
answered on 02 Sep 2009, 12:47 PM
Hi Srini,

The TreeViewItem is itself an ItemsControl, it has its own Items collection and ItemContainerGenerator as well.

If you define your items in Xaml, then getting the TreeView item is as simple as getting the item and casting it:

var treeViewItem = otherTreeViewItem.Items[0] as RadTreeViewItem;

Then, if you are binding the TreeView you will need to use the ItemContainerGenerator as you have done in your code sample.

Sincerely yours,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
Srini
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
Srini
Top achievements
Rank 1
Share this question
or