RadTreeViewItem Expand and Select

7 posts, 0 answers
  1. Amit
    Amit avatar
    4 posts
    Member since:
    Jan 2009

    Posted 06 Jan 2009 Link to this post

    Hi Guys

    I have a treeview which uses the LoadOnDeman in order to populate each layer via a WCF service. If I add a new child node programmatically then I'm not able Expand and Select the child node.

    RootParent
            -------Parent Object 
                      ------Child Object     <== this one is added programmatically
           --------Parent Object
           --------Parent Object


    I'm able to Expand the RootParent: But when I try to fetch the Parent node within the RootParent like this:

    RadTreeViewItem rootNode = RadTreeView1.ItemContainerGenerator.ContainerFromItem(RootParent) as RadTreeViewItem;
    rootNode.IsExpanded = true;

    RadTreeViewItem parentNode = RadTreeView1.ItemContainerGenerator.ContainerFromItem(parent) as RadTreeViewItem;

    returns null into parentNode.


    =====
    However, if I explicitly call LoadOnDemand on "parent node" after Expanding the RootParent, it works fine.

    RadTreeViewItem rootNode = RadTreeView1.ItemContainerGenerator.ContainerFromItem(RootParent) as RadTreeViewItem;
    rootNode.IsExpanded = true;

    LoadOnDemandMethodCall(parentDataObject)  <== this is a method we have which calls the WCF service to fetch data for the given object

    RadTreeViewItem parentNode = RadTreeView1.ItemContainerGenerator.ContainerFromItem(ParentObject) as RadTreeViewItem;
    parentNode.IsExpanded = true;

    ------------------

    Is it not possible to just find and expand a parentNode which would automatically trigger the LoadOnDemand event - rather then explicitly calling LoadOnDemand before attempting to Expand parentNode??

    Does setting parentNode.IsExpanded = true, in code behind, fire LoadOnDemand event? In my case I'm not able to find the parentNode to expand without loading data into the node first.

    Could you please help me out here?

    I've also tried to use RadTreeViewItem.ContainerFromItemRecursive(parentNode) <== this also returns null without executing LoadOnDemand first.

  2. Amit
    Amit avatar
    4 posts
    Member since:
    Jan 2009

    Posted 07 Jan 2009 Link to this post

    Hi again,

    Just to add to the above scenario - I've gone through some articles about  checking the ItemContainerGenerator.Status.
    If the Treeview child containers have not been generated, we must listen to the StatusChanged event until they are.

    So in the above scenario :

    TreeViewItem parentNode = TreeViewControl.ItemContainerGenerator.ContainerFromItem(parentObject) as TreeViewItem;

    and then we can check the Status of the container.....

    if (parentNode.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)

    but once again - the parentNode treeViewItem is null. We're not able to find it in the treeView eventhough the RootParent is Expanded.

    It seems like unless we Load the data into parentNode, by executing LoadOnDemand, we can't find that treeViewItem.

    Is there a way to get the TreeViewItem before we trigger LoadOnDemand?

    What I want to achieve is - to trigger LoadOnDemand event by Expanding the parentNode treeViewItem.
  3. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 07 Jan 2009 Link to this post

    Hi Amit,

    I am sending you a sample project demonstrating how to get the item you want without first calling LoadOnDemand. Your mistake is that you are always calling the ItemContainerGenerator of the TreeView, while the second time you need to call it on the RootNode instead. Please take a look at the attached project and let me know if it solves your problem.

    Greetings,
    Tihomir Petkov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  4. Amit
    Amit avatar
    4 posts
    Member since:
    Jan 2009

    Posted 07 Jan 2009 Link to this post

    Hi Tihomir

    Thanks for the sample code. It helped, but has not solved my problem.

    I did some changes to my code, and somehow my code does not trigger LoadOnDemand, the ItemContainerGenerator.Status comes back as GeneratorStatus.NotStarted.

    This is what I've done to Expand treeViewItem. I've used extension method which is called from the MainPage where we have the LoadOnDemand handler.

    public static void ExpandItem(this RadTreeView tvClientGroup, object itemToExpand, Telerik.Windows.Controls.ItemsControl Category)
            {
                ExpandItem(itemToExpand, Category);
            }

    private static bool ExpandItem(object itemToExpand, Telerik.Windows.Controls.ItemsControl Category)
            {

                foreach (object specification in Category.Items)
                {
                    if (specification == itemToExpand)
                    {
                        if (Category.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
                        {
                            var spec = Category.ItemContainerGenerator.ContainerFromItem(specification) as RadTreeViewItem;
                            if (spec != null)
                            {
                                spec.IsExpanded = true;
                                spec.IsSelected = true;
                                spec.BringIntoView();
                                return true;
                            }
                        }
                        else
                        {
                            EventHandler e = null;
                            e = new EventHandler(
                            delegate
                            {
                                if (Category.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
                                {
                                    if (ExpandItem(itemToExpand, Category) == false)
                                    {
                                    }
                                    Category.ItemContainerGenerator.StatusChanged -= e;
                                }
                            });
                            Category.ItemContainerGenerator.StatusChanged += e;
                        }
                    }
                }

         return false;
    }




  5. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 08 Jan 2009 Link to this post

    Hello Amit,

    I am not quite sure what exactly you're trying to do but in general LoadOnDemand is fired only when an item has no children. If this doen't help you, please make a small project in which you isolate your issue and send it to us so that we can take a look.

    Kind regards,
    Tihomir Petkov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Amit
    Amit avatar
    4 posts
    Member since:
    Jan 2009

    Posted 08 Jan 2009 Link to this post

    Thanks Tihomir, I've found out why my code does not trigger LoadOnDemand.

    Like you said "....in general LoadOnDemand is fired only when an item has no children."  I add an item to the parentNode and then Expand it - which does not trigger the LoadOnDemand because there is one item already added to it. I think I can now find a work around this problem.

    Thank you for your help.
  7. Michal
    Michal avatar
    1 posts
    Member since:
    Jun 2010

    Posted 24 Jun 2010 Link to this post

    Hi, I had a simmilar problem. I have a RadTreeView inside my component. Data of tree are binded with HierarchicalDataTemplate. Based on another data binded to my component, I want to make checkbox selection on some of the tree leaves.
    Many thanks to Amid, I could make a workarround.

        public static class RadTreeViewExtensions 
        { 
            #region ToggleTag 
     
            public static void ToggleTag(this RadTreeView radTreeView, ContactTag tagToToggle, ToggleState toggleState) 
            { 
                ToggleTag(tagToToggle, toggleState, radTreeView); 
            } 
     
            private static Boolean ToggleTag(ContactTag tagToToggle, ToggleState toggleState, ItemsControl itemsControl) 
            { 
                foreach (var obj in itemsControl.Items) 
                { 
                    if (itemsControl.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) 
                    { 
                        var rtvItem = itemsControl.ItemContainerGenerator.ContainerFromItem(obj) as RadTreeViewItem; 
                        if (rtvItem != null
                        { 
                            rtvItem.IsExpanded = true
                            rtvItem.IsSelected = true
                            rtvItem.BringIntoView(); 
                            if (obj is ContactTagClass) 
                            { 
                                var tagClass = obj as ContactTagClass; 
                                if (tagClass.Oid == tagToToggle.TagClassOid) 
                                { 
                                    rtvItem.CheckState = toggleState; 
                                    return true
                                } 
                            } 
                            else 
                            { 
                                if (ToggleTag(tagToToggle, toggleState, rtvItem)) 
                                { 
                                    return true
                                } 
                            } 
                        } 
                    } 
                    else 
                    { 
                        EventHandler e = null
                        e = new EventHandler( 
                            delegate 
                                { 
                                    if (itemsControl.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) 
                                    { 
                                        if (ToggleTag(tagToToggle, toggleState, itemsControl) == false
                                        { 
                                        } 
                                        itemsControl.ItemContainerGenerator.StatusChanged -= e; 
                                    } 
                                }); 
                        itemsControl.ItemContainerGenerator.StatusChanged += e; 
                    } 
                } 
                return false
            } 
            #endregion 
        } 
     

Back to Top