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

How to create select/deselect all checkbox?

3 Answers 193 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
fred williams
Top achievements
Rank 1
fred williams asked on 11 Apr 2011, 07:14 PM
Hello,

I'm trying to add an item to the top of my treeview with a value of "Select/Deselect All" that will check all items. I have a treeview named "tvDirectory" 

I added it like this:
tvDirectory.Items.Add("Select/Deselect All");
 

Then I tried to implement the functionality in the Checked eventhandler like this:
RadTreeViewItem item = (RadTreeViewItem)((RadTreeView)sender).SelectedContainer;
if (item != null)
{
 
    if (item.CheckState == ToggleState.On)
    {
        foreach (RadTreeViewItem tvItem in tvDirectory.Items)
        {
            tvItem.CheckState = ToggleState.On;
        }
    }
    else
    {
        foreach (RadTreeViewItem tvItem in tvDirectory.Items)
        {
            tvItem.CheckState = ToggleState.Off;
        }
    }
}

But I get an "object reference not set to instance of an object for  "item". Help please, thanks.

3 Answers, 1 is accepted

Sort by
0
Accepted
Kiril Stanoev
Telerik team
answered on 12 Apr 2011, 01:30 PM
Hello Fred,

I wasn't able to reproduce the issue you describe. Please take a look at my test project and let me know if I am missing something. I'd be glad to further assist you.

Best wishes,
Kiril Stanoev
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
0
fred williams
Top achievements
Rank 1
answered on 12 Apr 2011, 02:38 PM
Hello and thanks for the reply.

I've added your check and uncheck event handlers to my code, but when I run it I get an error :

"Unable to cast item of type System.String to type Telerik.Windows.Controls.RadTreeViewItem"
0
fred williams
Top achievements
Rank 1
answered on 12 Apr 2011, 03:14 PM
Hello again,

I was able to get your code working. The problem I had was that I was declaring th eother items in the treeview with text values:
RadTreeViewItem item = new RadTreeViewItem("value here");

Instead of this way:
RadTreeViewItem root = new RadTreeViewItem() { Header = "Select/Deselect All" };


and that's why I was getting the string error. Thanks for all of your help.
Tags
TreeView
Asked by
fred williams
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
fred williams
Top achievements
Rank 1
Share this question
or