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

Iterate through checkeditems

6 Answers 155 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 24 Nov 2009, 02:13 PM
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?

6 Answers, 1 is accepted

Sort by
0
Accepted
Kiril Stanoev
Telerik team
answered on 24 Nov 2009, 03:27 PM
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.
0
Nick
Top achievements
Rank 1
answered on 25 Nov 2009, 10:27 AM
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
0
Accepted
Kiril Stanoev
Telerik team
answered on 26 Nov 2009, 08:34 AM
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.
0
Nick
Top achievements
Rank 1
answered on 27 Nov 2009, 12:54 PM
Hi Kiril,

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

Nick
0
Richardsonke
Top achievements
Rank 1
answered on 22 Jan 2010, 06:43 PM
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
0
Kiril Stanoev
Telerik team
answered on 26 Jan 2010, 11:51 AM
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.
Tags
TreeView
Asked by
Nick
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Nick
Top achievements
Rank 1
Richardsonke
Top achievements
Rank 1
Share this question
or