How to get RadTreeViewItem by tag

4 posts, 1 answers
  1. Srini
    Srini avatar
    41 posts
    Member since:
    Jul 2009

    Posted 02 Sep 2009 Link to this post

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

  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 02 Sep 2009 Link to this post

    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.
  3. DevCraft banner
  4. Srini
    Srini avatar
    41 posts
    Member since:
    Jul 2009

    Posted 02 Sep 2009 Link to this post

    Thanks for your reply..

    Can u tell me how to get the child root items of a RadTreeViewItem...
  5. Answer
    Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 02 Sep 2009 Link to this post

    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.
Back to Top