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

Finding no way to search child node of a RadTreeViewItem

8 Answers 458 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
sonal
Top achievements
Rank 1
sonal asked on 14 Dec 2008, 01:08 PM
Hi,

Continuing the same example as was in the previous query.

<telerik:RadTreeView HorizontalAlignment="Left"
                             x:Name="uxOrgTree"
                             Width="Auto"
                             Height="Auto"
                             d:LayoutOverrides="Height"  
                             VerticalAlignment="Top"
                             Margin="0,0,0,0"
                             SelectionMode="Single"
                             IsEditable="True"
                             ItemsSource="{Binding RootNodes}" >
            <telerik:RadTreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type Entities:Node}" ItemsSource="{Binding Children}" >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path= Name}"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </telerik:RadTreeView.Resources>
      <telerik:RadTreeView.ItemContainerStyle>
                <Style TargetType="{x:Type telerik:RadTreeViewItem}">
                    <EventSetter Event="Edited" Handler="RadTreeViewItem_Edited" />
                 </Style>
            </telerik:RadTreeView.ItemContainerStyle>
        </telerik:RadTreeView>

I am trying to do something like styled as bold, in the code behind:

 private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
               RadTreeViewItem currentItem = sender as RadTreeViewItem;
                if (currentItem != null)
                {
                    currentItem.ContextMenu.Tag = currentItem;
                }
      }

  private void MenuItem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;
            if (menu != null)
            {
                  ContextMenu contextMenu = this.FindResource("uxContextMenu") as ContextMenu;

                    Debug.Assert(contextMenu != null, "contextMenu == null", "null contextMenu");
                    RadTreeViewItem currentItem = contextMenu.Tag as RadTreeViewItem;
                   
                   //looking for 5th child of the currentItem, not sure how to get children if a treeview item.
                    currentItem.Children[5] as RadTreeViewItem;

                       
             }
        }

Thanks
Sonal

8 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 15 Dec 2008, 05:45 PM
Hi Sonal,

You should use the Items collections, not the Children collections, since the RadTreeViewItem is an ItemsControl.

Sincerely yours,
Valentin.Stoychev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
sonal
Top achievements
Rank 1
answered on 16 Dec 2008, 04:54 AM
Hi,
Considering the example in the original post, in the MenuItem_Click handler if i try to typecast item as RadTreeViewItem

currentItem.Items[5] as RadTreeViewItem, it gives null.

I am unable to access child node of a RadTreeViewItem.

Thanks
Sonal
0
Miroslav
Telerik team
answered on 17 Dec 2008, 11:50 AM
Hello Sonal,

The TreeView (or any ItemsControl actually) will return the items you have used to generate it. In this case you specify ItemsSource, which is a collection of Business objects. Then the tree view will generate controls that will wrap them and present them.

You can access the items though the item collection, but if you need their containers, you have to use the ItemContainerGenerator and ask for the containers. Please note that the containers will not be generated until needed (until visible), but you can handle the ItemContainerGenerator.StatusChanged event and be notified when they are generated.

Example code for getting the containers:

//When the item is a direct child of the TreeView:  
uxOrgTree.ItemContainerGenerator.ContainerFromItem(myNode);  
 
//When the item is a descendant of the TreeView (i.e. child of a child...)  
uxOrgTree.ContainerFromItemRecursive(myNode); 

Does this work for you?

All the best,
Miroslav
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Hussein
Top achievements
Rank 1
answered on 11 Jan 2012, 01:36 PM
Hi,

When I use this for a new created items in the treeview I got null
 
//When the item is a descendant of the TreeView (i.e. child of a child...)  
uxOrgTree.ContainerFromItemRecursive(myNode); 

while myNode is a business object, just created and added to the collection to which the treeview bind to.

Any advice?
Thanks;
0
Tina Stancheva
Telerik team
answered on 11 Jan 2012, 01:55 PM
Hi Hussein,

If the ContainerFromItemRecursive() method returns null that means that either the item isn't in the ItemsSource of the RadTreeView  or the containers aren't yet generated.

Basically you need to keep in mind that this method can only be used when the RadTreeView.ItemContainerGenerator has finished generating the containers for the business items. You can handle the ItemContainerGenerator.StatusChanged event to get the container of an item only if the status of the ItemContainerGenerator is ContainersGenerated:
BusinessItem child;
...
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
    if ((sender as Telerik.Windows.Controls.ItemContainerGenerator).Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
    {
        RadTreeViewItem childContainer = treeView.ContainerFromItemRecursive(child);
    }
}


However, as I am not sure how you use the ContainerFromItemRecursive() method, I am not sure what is causing your issues. But if you can send us a sample demonstrating your scenario, we'll be able to further investigate your case.

Greetings,
Tina Stancheva
the Telerik team

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

0
Hussein
Top achievements
Rank 1
answered on 11 Jan 2012, 02:52 PM
Hi Tina,

Here is a sample code of the issue I'm facing


private void AddNewItemExecute(RadContextMenu menu)
        {
            if (menu == null)
                return;
 
            RadTreeViewItem clickedContainer = menu.GetClickedElement<RadTreeViewItem>();
            if (clickedContainer != null)
            {
                bizobj d = clickedContainer.Item as bizobj;
 
                bizobj newobj = new bizobj();
                newobj.ID = 10;
                newobj.Name = "undefined";
 
                if (d.ChildNodes == null)
                    d.ChildNodes = new ObservableCollection<bizobj>();
 
                d.ChildNodes.Add(newobj);
 
                //this line returns null always
                clickedContainer.ParentTreeView.ContainerFromItemRecursive(newobj);
 
                clickedContainer.IsExpanded = true;
                clickedContainer.ParentTreeView.SelectedItem = newobj;
                TreeViewItemClickCommand.Execute(newobj);
            }
        }

        }
0
Tina Stancheva
Telerik team
answered on 16 Jan 2012, 09:56 AM
Hello Hussein,

The ContainerFromItemRecursive() method returns null since the newobj RadTreeViewItem container isn't yet generated. Basically the RadTreeView generates a container for a data item when it has to display that item. But in your code as you have just added the new data item, the RadTreeView hasn't generated its container and this is why you can't access it.

From your code snippet I can't see what you need to use the newobj RadTreeViewItem container for. However, a better approach would be to use data binding if you need to set any of the new item properties. For example if you want to set the selected state of the new item, it's better to bind the IsSelected property of the RadTreeViewItems and use a business property to control it. This approach will allow you to control the state of a newly added data item without having to get its RadTreeViewItem container. Can you try this approach and let me know if it works for you?

All the best,
Tina Stancheva
the Telerik team

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

0
Hussein
Top achievements
Rank 1
answered on 16 Jan 2012, 01:00 PM
It works, Thank you very much for your help :)
Hussein
Tags
TreeView
Asked by
sonal
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
sonal
Top achievements
Rank 1
Miroslav
Telerik team
Hussein
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or