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

[Solved] How to get a radTreeView from a node

6 Answers 310 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chad
Top achievements
Rank 1
Chad asked on 12 Feb 2009, 06:10 PM
Hi,

I'm trying to get the radTreeViewItem for all nodes from my tree using a loop, so that I can change the background of particular nodes, but I can't figure out how to get the radTreeViewItem.  The "Tree.CheckedItems" gets the RadTreeViewItem so that I can do this, but I need to do it on everything, not just the checked nodes.  I also tried to set the checkbox on all nodes to checked, temporarily, so that I can use the CheckedItems function to get the RadTreeViewItems, but I don't know how to go about that either. 

  foreach (object item in treeWells.CheckedItems)
            {
                treeItem = (RadTreeViewItem)item;
              
                
                if (item.ToString().Equals("WTXWebSilver.Well"))
                {

                    well = (Well)treeItem.Item;

                    if (wellList.Contains(well.ID))
                    {
                        treeItem.Background = new SolidColorBrush(Colors.Red);
                    }

                   else {
                            treeItem.Background = new SolidColorBrush(Colors.White);
                          }

                }

            }

Thanks

6 Answers, 1 is accepted

Sort by
0
Tihomir Petkov
Telerik team
answered on 13 Feb 2009, 07:42 AM
Hi Chad,

You can get the TreeView items by first subscribing to the treeView.ItemContainerGenerator.StatusChanged event. Then, in the handler method you do the following:

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
 if ((sender as ItemContainerGenerator).Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
        {
         RadTreeViewItem item = treeView.ItemContainerGenerator.ContainerFromItem(yourItem) as RadTreeViewItem;
        }
}

Let me know if this solves your problem.

Best wishes,
Tihomir Petkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Chad
Top achievements
Rank 1
answered on 13 Feb 2009, 04:22 PM
hmm, ok I'll try it, that will probably work, but it requires that the tree be changed in some way, to get the even fired, is this right?  I don't change the tree, I click a button, which in turn changes the tree, so I'm not sure if this will work. 

I hope I explained it well enough.

Thanks
0
Bobi
Telerik team
answered on 16 Feb 2009, 09:53 AM
Hi Chad,

To have access to the RadTreeView items you need to subscribe to the RadTreeView.ItemContainerGenerator.StatusChanged event in the constructor of your application page. You will not need to do anything else if you want to get a particular item. You can simply use something like:

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)  
        {  
            if ((sender as ItemContainerGenerator).Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)  
            {  
                RadTreeViewItem item = tree.ItemContainerGenerator.ContainerFromItem(this.TargerItem) as RadTreeViewItem;  
                item.Background = new SolidColorBrush(Colors.Green);  
            }  
        }  
 

If you want to change the background of a particular item in the button click event (not automatically  setting the event in the constructor) you can use:

private void Button_Click(object sender, RoutedEventArgs e)  
        {  
            if (tree.ItemContainerGenerator.Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)  
            {  
                RadTreeViewItem item = tree.ItemContainerGenerator.ContainerFromItem(this.TargetPage) as RadTreeViewItem;  
                item.Background = new SolidColorBrush(Colors.Green);  
            }  
        } 


All the best,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Chad
Top achievements
Rank 1
answered on 18 Feb 2009, 12:37 AM
Thanks for the quick replies.

I'm slowly getting to the solution, what you said does work, however, I can only seem to get it to work on the first level of my tree.

Here's my code:
            if (treeWells.ItemContainerGenerator.Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
            {

                

                foreach (object item in treeWells.Items)
                {
                    treeItem = treeWells.ItemContainerGenerator.ContainerFromItem(item) as RadTreeViewItem;

                    if (treeItem.Items != null)
                    {
                        foreach (object batteries in treeItem.Items)
                        {

                            if (treeItem.ItemContainerGenerator.Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                            {


                                batteryItem = treeWells.ItemContainerGenerator.ContainerFromItem(batteries) as RadTreeViewItem;

                                if (batteryItem != null)
                                {
                                    foreach (object wellsItem in batteryItem.Items)
                                    {
                                        if (batteryItem.ItemContainerGenerator.Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                                        {

                                            if (wellsItem != null)
                                            {
                                                wellItem = treeWells.ItemContainerGenerator.ContainerFromItem(wells) as RadTreeViewItem;

                                                treeItem.Background = new SolidColorBrush(Colors.White);

                                                well = (Well)treeItem.Item;

                                                if (wellList.Contains(well.ID))
                                                {
                                                    treeItem.Background = new SolidColorBrush(Colors.Red);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    
                }
            }

it's a bit of a mess.  The batteries keep coming back as null, even when I know I have expanded them.  I'm assuming I need to make a

ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged); event for every level of the tree.  Is this correct?   If so, how do I go about doing this, I need to be able to get the RadTreeViewItem for Areas->Batteries->Wells.

Thanks,
Chad

0
Chad
Top achievements
Rank 1
answered on 19 Feb 2009, 06:05 PM
I have found a solution at:
http://www.telerik.com/community/forums/silverlight/treeview/radtreeviewitem-expand-and-select.aspx.

thanks for the help, next time I'll look a bit harder for the answer.

Chad
0
Bobi
Telerik team
answered on 20 Feb 2009, 08:16 AM
Hi Chad,

I am glad to hear that you found a solution to your problem .

Sincerely yours,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
TreeView
Asked by
Chad
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
Chad
Top achievements
Rank 1
Bobi
Telerik team
Share this question
or