RadTreeView suggestion

15 posts, 0 answers
  1. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 24 Jun 2008 Link to this post

    It would be nice to have properties like AutoCheckChildren and AutoSelectChildren. At the moment I cannot see any other way to achieve this than to disable the built-in checkboxes and use my own checkboxes in the item template, which seems overly complicated.
  2. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 24 Jun 2008 Link to this post

    Hello Henrik,

    You can achieve this if you handle the Selected/Checked event and select/check all the child items of the source item.

    Will this work for your scenario?

    Best wishes,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. DevCraft banner
  4. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 24 Jun 2008 Link to this post

    There must be something fundamentally wrong with my thinking here... I can't even iterate through the child items programmatically. When I access  RadTreeViewItem.Items all I get is the original data objects, and I can't seem to cast these to RadTreeViewItem objects so that I can uncheck them. Do you have some sample code that iterates through all child nodes for a given node (preferably recursive)? Thanks, /Henrik
  5. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 25 Jun 2008 Link to this post

    Yes - this is a major and very important concept of the Silverlight/WPF ItemsControl. It is not related to the implementation of the RadTreeView, but you will see it in any control that derives from ItemsControl (another example is the ListBox).

    The Items collection of the ItemsControl is a collection of data objects , NOT from RadTreeViewItems. There is a very important concept in Silverlight/WPF about the items and item containers. When you bind an ItemsControl you have your data items populating the Items collection of the control. The containers are the visual presentations of those data items (in our case the containers are the RadTreeViewItems). Containers are not created automatically when the ItemsControl is bound, but are created asyncronously and only when needed. For example in our case with the TreeView, the containers are not being created until there parent is expanded.

    So how you can get the container from the data item? Or in our case - how to get the RadTreeViewItem from your data object that you receive when traversing the Items collection?

    There are several helper methods that can be used. Each ItemsControl have a Property named ItemContainerGenerator. It is of type ItemContainerGenerator . This class has the following methods that can helps us in our case:

     - DependencyObject ContainerFromIndex(int index) - returns the Container for the given index from the Items collection.

     - DependencyObject ContainerFromItem(object item) -  returns the Container for the given data item from the Items collection.

    So here is an example about how to get access to all the Containers in the RadTreeView:

      void getContainers()  
            {  
     
                // gets all nodes from the TreeView  
                Collection<RadTreeViewItem> allTreeContainers = GetAllItemContainers(tree1);  
     
                // gets all nodes (recursively) for the first node  
                RadTreeViewItem firstNode = tree1.ItemContainerGenerator.ContainerFromIndex(0) as RadTreeViewItem;  
                if (firstNode != null)  
                {  
                    Collection<RadTreeViewItem> firstNodeContainers = GetAllItemContainers(firstNode);  
                }  
     
     
            }  
     
            Collection<RadTreeViewItem> GetAllItemContainers(Telerik.Windows.Controls.ItemsControl itemsControl)  
            {  
     
                Collection<RadTreeViewItem> allItems = new Collection<RadTreeViewItem>();  
     
                for (int i = 0; i < itemsControl.Items.Count; i++)  
                {  
                    // try to get the item Container  
                    RadTreeViewItem childItemContainer = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as RadTreeViewItem;  
     
                    // the item container maybe null if it is still not generated from the runtime  
                    if (childItemContainer != null)  
                    {  
                        allItems.Add(childItemContainer);  
     
                        Collection<RadTreeViewItem> childItems = GetAllItemContainers(childItemContainer);  
                        foreach (RadTreeViewItem childItem in childItems)  
                        {  
                            allItems.Add(childItem);  
                        }  
                    }  
                }  
                return allItems;  
            }  
     

    I hope this makes the picture a little more clear. These concepts are all borowed from WPF and it is hard to understand without to play with the ItemsControl for some time. I can recommend the following great blogs posts which are written for WPF, but they explain the concept very well and will be of help for the Silverlight learning:
    http://blog.quantumbitdesigns.com/2008/05/22/programmatically-selecting-an-item-in-a-treeview/
    http://www.drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx - all posts regarding the ItemsControl.

    Please let us know if you still have any questions regarding those concepts!

    All the best,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 25 Jun 2008 Link to this post

    Now I get it. This is beginning to make sense now. Thank you very much for taking your time to explain this.
  7. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 25 Jun 2008 Link to this post

    I'm afraid I cannot get the example code working. ItemContainerGenerator doesn't seem to be available in the CTP. Perhaps this is something that will be available in the July beta? I cannot find it in System.Windows.Controls.ItemsControl either.

    I did get something similar working though after some studying (thanks for the links), and I have to say I still think there's a need for the AutoSelectChildren and AutoCheckChildren properties. In my application I need to switch between these two modes, and the current checkbox selection implementation is much too limited for my purposes. For example, it is not possible to check a single node in the middle of a branch. As soon as all children of a node are unchecked the node itself will be unchecked too, and that doesn't work for me.

  8. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 25 Jun 2008 Link to this post

    Hello Henrik,

    Yes this is my fault - we changed the API after the CTP in order to be WPF compatible. I'm glad you managed to convert the code.

    For the checkboxes issue  - maybe I'm not getting something but if the RadTreeView.IsTristateMode property is set to false you should be able to check any node in the hierarchy without this to affect the rest of the nodes?

    Greetings,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  9. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 25 Jun 2008 Link to this post

    Ah, yes, of course. My mistake. I've always had IsTriState set to true. It does seem kind of illogical though that the tree selection mode is affected in this way when IsTriState is true. What the property in fact does is effectively the same as AutoCheckChildren=true. Maybe you could separate IsTriState into two properties? Even if one doesn't want children automatically checked it's still nice to get the Indeterminate state for the parent checkboxes of a checked node. I hope I'm making myself clear. Anyway, thanks again for being patient with me. :)
  10. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 25 Jun 2008 Link to this post

    Hi Henrik,

    --- qte---
    It does seem kind of illogical though that the tree selection mode is affected in this way when IsTriState is true
    ---- /qte ---

    The tree selection should not be affected by the IsTristateMode property - this sounds more like a bug. Can you please tell us what is the exact change in selection behavior when changing the property? The change in the property should affect only how the checkboxes state is being populated.

    Greetings,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  11. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 25 Jun 2008 Link to this post

    Sorry, what I meant was "checkbox behaviour". I've posted a screen shot here to illustrate my point:

    http://cid-cc43a90a79374ebd.skydrive.live.com/self.aspx/Public/TreeViewExample.png

    When IsTriState is True, it doesn't matter whether I check "Salmon", "FloralWhite" or "CornflowerBlue", all three are checked automatically. The behaviour I would like is as follows:
    When FloralWhite is checked, that node and only that node should be checked, but all its parent nodes should display the indeterminate state.
    I would also like to have an AutoCheckChildren property. When this is True, and FloralWhite is checked, CornflowerBlue should also be checked, but not Salmon. Still, if IsTriState is True, all parent nodes should display the indeterminate checkbox state.
    At the moment, if I check FloralWhite with IsTriState set to False and then change IsTriState to True, the node will automatically be unchecked. Which is not what I want. The indeterminate state is a great way of indicating to the user that something is selected at a deeper level in the tree, but I do not want that visual cue to affect the way the checkboxes behave when checked.

  12. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 25 Jun 2008 Link to this post

    Hello Henrik,

    I understand your scenario - yes currently this is not possible. Will record this request and if there are more requests for such a functionality we can embed it inside the control. For the moment you can think of some custom logic to handle the PreviewChecked event and to stop the propagation of the check state for the child items.

    All the best,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  13. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 25 Jun 2008 Link to this post

    Ok, so there will definitely be a PreviewChecked event in the final release? Because in the CTP release I can find no such event.
  14. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 25 Jun 2008 Link to this post

    Yes - my bad again, the next release is scheduled for 11.July.

    Greetings,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  15. Senthil Subramanian
    Senthil Subramanian avatar
    10 posts
    Member since:
    Jun 2010

    Posted 09 Nov 2010 Link to this post

    Hello,

         I am using the latest version of Telerik RadControls(2010.2.0924). I am trying to address the same requirement mentioned in this post. Are there any fixes/updates regarding the same in the above mentioned version?

    Thanks and Regards,
    Senthil
  16. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 12 Nov 2010 Link to this post

    Hi Senthil Subramanian,

    We have implemented PreviewChecked event. Can you use it to implement your scenario?

    All the best,
    Hristo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
DevCraft banner