Iterate through checkeditems

7 posts, 2 answers
  1. Nick
    Nick avatar
    5 posts
    Member since:
    Jun 2006

    Posted 24 Nov 2009 Link to this post

    I am trying to iterate through the radtreeview's CheckedItems collection but when i try and cast to a RadTreeViewItem to look at the CheckedState (for "On" as i am using tristate) it errors saying that it cannot cast to this type. 

    I have had a search on this forum and all i seem to be able to find is something to do with the ItemContainerGenerator.  However, when i call ContainerFromItem(item) (item being an object from the CheckedItems collection) it returns null.  The data is coming from a WCF service and is LoadedOnDemand.

    ...what am i doing wrong? why cant i just cast the objects from the CheckedItems collection?
  2. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 24 Nov 2009 Link to this post

    Hi Nick,

    The reason why ItemContainerGenerator returns null is because the items have not been visible yet. I have prepared couple of demos. The first demo iterates through the items in the treeview and asks the container of the item if it is checked. The second demo uses ContainerBindingsCollection and moves the checked logic inside the business object, which in my opinion creates a cleaner code. Please, evaluate both projects and let me know if you have additional questions.

    Greetings,
    Kiril Stanoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Nick
    Nick avatar
    5 posts
    Member since:
    Jun 2006

    Posted 25 Nov 2009 Link to this post

    Hi Kiril,

    Thank you for providing these examples they are just what i needed. 

    While i have your attention could you tell me if there is a way to (using loadondemand) load all the children when a parent node is checked.  The reason i ask is (looking at your example) i only need to know the family members when a family name is checked, but if the members arent loaded and the family name is checked how do i know who they are?  I am working with 3 levels of data and only require the 3rd level.

    Thanks again

    Nick
  4. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 26 Nov 2009 Link to this post

    Hi Nick,

    The LoadOnDemand feature of RadTreeView applies only when the node is expanded. Have a look at our online LoadOnDemand example for further reference. Populating a family with members when this family is checked is another story. First, you will have to register a handler for RadTreeViewItem's routed event CheckedEvent:

    this.AddHandler(RadTreeViewItem.CheckedEvent, new EventHandler<Telerik.Windows.RadRoutedEventArgs>(treeViewItem_Checked), false);

    The event handler(treeViewItem_Checked) will get invoked every time an item is checked, no matter if it is of type Family or Member. Therefore, you will have check for the type of the item and if it is of type Family, then load the members of that family:

    private void treeViewItem_Checked(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        RadTreeViewItem item = e.OriginalSource as RadTreeViewItem;
        if(item.DataContext is Family)
        {
            Family family = item.DataContext as Family;
            family.LoadChildren();
        }
    }

    I have changed the business model a little bit so it fits the scenario better. Basically, that is the idea. Have a look at the attached sample project and let me know if you have additional questions or something is unclear.

    Regards,
    Kiril Stanoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Nick
    Nick avatar
    5 posts
    Member since:
    Jun 2006

    Posted 27 Nov 2009 Link to this post

    Hi Kiril,

    Thanks again for another useful example, that worked for what i wanted.

    Nick
  6. Richardsonke
    Richardsonke avatar
    93 posts
    Member since:
    Jul 2007

    Posted 22 Jan 2010 Link to this post

    Is it possible to use this with TriStateMode so if I check a child item, the parent has the indicator that some but not all items are checked?  Thanks.

    -Keith
  7. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 26 Jan 2010 Link to this post

    Hi Keith,

    You should be able to apply the previous approach to a TriStateMode scenario. However, currently RadTreeView has some issues with regard to TriStateMode and ContainerBindings and we are working on fixing these problems. However, I cannot bind to a specific date when the fix will be available. Let me know if you have additional questions on the matter.

    Kind regards,
    Kiril Stanoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top