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

Disabled Children Nodes Checked on Root Selection

2 Answers 117 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Xorcist
Top achievements
Rank 1
Xorcist asked on 22 Sep 2015, 04:50 PM

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 Answers, 1 is accepted

Sort by
0
Xorcist
Top achievements
Rank 1
answered on 22 Sep 2015, 05:57 PM

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;
}

0
Hristo
Telerik team
answered on 23 Sep 2015, 08:05 AM
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
Tags
Treeview
Asked by
Xorcist
Top achievements
Rank 1
Answers by
Xorcist
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or