How to create select/deselect all checkbox?

4 posts, 1 answers
  1. fred williams
    fred williams avatar
    55 posts
    Member since:
    Feb 2010

    Posted 11 Apr 2011 Link to this post

    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.

  2. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 12 Apr 2011 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. fred williams
    fred williams avatar
    55 posts
    Member since:
    Feb 2010

    Posted 12 Apr 2011 Link to this post

    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"
  5. fred williams
    fred williams avatar
    55 posts
    Member since:
    Feb 2010

    Posted 12 Apr 2011 Link to this post

    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.
Back to Top