Finding no way to search child node of a RadTreeViewItem

9 posts, 0 answers
  1. sonal
    sonal avatar
    15 posts
    Member since:
    Jul 2008

    Posted 14 Dec 2008 Link to this post

    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
  2. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 15 Dec 2008 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. sonal
    sonal avatar
    15 posts
    Member since:
    Jul 2008

    Posted 15 Dec 2008 Link to this post

    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
  5. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 17 Dec 2008 Link to this post

    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.
  6. Hussein
    Hussein avatar
    5 posts
    Member since:
    Jan 2012

    Posted 11 Jan 2012 Link to this post

    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;
  7. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 11 Jan 2012 Link to this post

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

  8. Hussein
    Hussein avatar
    5 posts
    Member since:
    Jan 2012

    Posted 11 Jan 2012 Link to this post

    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);
                }
            }

            }
  9. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 16 Jan 2012 Link to this post

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

  10. Hussein
    Hussein avatar
    5 posts
    Member since:
    Jan 2012

    Posted 16 Jan 2012 Link to this post

    It works, Thank you very much for your help :)
    Hussein
Back to Top
UI for WPF is Visual Studio 2017 Ready