Disabled Children Nodes Checked on Root Selection

3 posts, 0 answers
  1. Xorcist
    Xorcist avatar
    92 posts
    Member since:
    Apr 2009

    Posted 22 Sep 2015 Link to this post

    Using the following code: (Telerik UI for WinForms 2015.2.728)

    tvElements.CheckBoxes = true;
    tvElements.TriStateMode = true;
    RadTreeNode root = tvElements.Nodes.Add("Classes");
    foreach (CNFClass cls in CNFConf.Classes) {
      RadTreeNode parent = root.Nodes.Add(cls.UID.ToString(), cls.Code, null);
      parent.ToolTipText = cls.Desc;
      parent.Enabled = cls.Enabled;
    }

    Checking the root node will check all Enabled and all Disabled children nodes.

    Seems like this may be a bug, is there a workaround?

  2. Xorcist
    Xorcist avatar
    92 posts
    Member since:
    Apr 2009

    Posted 22 Sep 2015 in reply to Xorcist Link to this post

    I ended up working around this with a NodeCheckedChanging event handler as such:

    private void RadTreeView_NodeCheckedChanging(object sender, RadTreeViewCancelEventArgs e) {
      e.Cancel = !e.Node.Enabled;
    }

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 23 Sep 2015 Link to this post

    Hello John,

    Thank you for writing.

    Even if a node in RadTreeView is disabled it can be programmatically accessed and its Checked state changed. When RadTreeView is in TriStateMode, checking the root node is going to initiate an internal logic for iterating all child nodes and eventually all nodes will be iterated and correct checked state be calculated.

    The solution in your last post is valid and if it fits your scenario you can go ahead with it. An alternative approach which does not require handling of events in your form is to extend the RadTreeNode class and override the UpdateChildrenCheckState method. Please find below my sample implementation respecting whether a child node is enabled or not: 
    public class MyRadTreeNode : RadTreeNode
    {
        public MyRadTreeNode(string text)
            : base(text) { }
     
        public MyRadTreeNode(string text, Image image, bool expanded)
            : base(text, image, expanded) { }
     
        protected override void UpdateChildrenCheckState()
        {
            if (this.CheckType != Telerik.WinControls.UI.CheckType.CheckBox || (this.TreeViewElement != null && !this.TreeViewElement.TriStateMode))
            {
                return;
            }
     
            if (this.TreeViewElement != null && !this.TreeViewElement.AutoCheckChildNodes)
            {
                return;
            }
     
            if (this.TreeViewElement != null)
            {
                this.TreeViewElement.BeginUpdate();
            }
     
            int update = 0;
            Stack<RadTreeNodeCollection> nodeStack = new Stack<RadTreeNodeCollection>();
            nodeStack.Push(this.Nodes);
            while (nodeStack.Count > 0)
            {
                RadTreeNodeCollection nodes = nodeStack.Pop();
     
                foreach (MyRadTreeNode child in nodes)
                {
                    if (child.CheckType == CheckType.None || !child.Visible || !child.Enabled)
                    {
                        continue;
                    }
     
                    if (child.SetCheckStateCore(this.CheckState))
                    {
                        child.OnCheckStateChanged(CheckedMode.ParentChild);
                        update++;
                    }
     
                    if (child.Nodes.Count > 0)
                    {
                        nodeStack.Push(child.Nodes);
                    }
                }
            }
     
            if (this.TreeViewElement != null)
            {
                this.TreeViewElement.EndUpdate(update > 0, RadTreeViewElement.UpdateActions.StateChanged);
            }
        }
    }

    I am also sending you a gif file showing the result on my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top