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

Node in TriState mode get wrong CheckState.

1 Answer 141 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Peter Bogoyavlensky
Top achievements
Rank 1
Peter Bogoyavlensky asked on 17 May 2012, 09:18 AM
I use Telerik Version=2011.3.11.1219 and try use TreeView control with TriStateMode.

Question 1: Also I want to prevent checking node due some conditions - for example, if node.ImageKey ="class_grey" (some picture from imagelist container).

Private Sub RadTreeView1_NodeCheckedChanging(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RadTreeViewCancelEventArgs) Handles RadTreeView1.NodeCheckedChanging
  If Not e.Node.Checked AndAlso e.Node.ImageKey = "class_grey" Then
    e.Cancel = True
  End If
End Sub

If I make that tree:
 
and try to check node "Node1" - I get this situation:


But it is obviously wrong! Nodes "Node1" and "Node8" have wrong state mode ("On" instead of "Indeterminate")
It should be like that (which I got if uncheck/check node "Node9"):


How can I fix that?

Question 2: How can I change view of checkbox in TriState mode? For example, I would like to change view for "Indeterminate" mode from filled green box to red tick.

1 Answer, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 21 May 2012, 04:22 PM
Hi Peter,

Thank you for writing.

I managed to reproduce the issue. I logged it in PITS and it will be addressed in one of the next releases. Here is a link to the item, where you can add your vote for it, in order to increase its priority:  http://www.telerik.com/support/pits.aspx#/public/winforms/11162 

Meanwhile, you should use the NodeCheckedChanging and NodeCheckedChanged event to work around it:
int checkingCount = 0;
 
private void OnNodeCheckedChanging(object sender, RadTreeViewCancelEventArgs e)
{
    if (e.Node.Tag != null)
    {
        e.Cancel = true;
    }
    else
    {
        this.checkingCount++;
    }
}
 
private void OnNodeCheckedChanged(object sender, RadTreeViewEventArgs e)
{
    this.checkingCount--;
 
    if (this.checkingCount == 0)
    {
        this.CorrentParentStates(e.Node);
    }
}
 
private void CorrentParentStates(RadTreeNode radTreeNode)
{
    Stack<RadTreeNode> invalidParents = new Stack<RadTreeNode>();
    Stack<RadTreeNode> childNodes = new Stack<RadTreeNode>();
    childNodes.Push(radTreeNode);
 
    FieldInfo fieldInfo = typeof(RadTreeNode).GetField("checkState", BindingFlags.NonPublic | BindingFlags.Instance);
 
    while (childNodes.Count > 0)
    {
        RadTreeNode currentNode = childNodes.Pop();
        RadTreeNodeCollection nodes = currentNode.Nodes;
 
        foreach (RadTreeNode node in nodes)
        {
            if (node.CheckState != currentNode.CheckState && !invalidParents.Contains(currentNode))
            {
                invalidParents.Push(currentNode);
            }
 
            if (node.Nodes.Count > 0)
            {
                childNodes.Push(node);
            }
        }
    }
 
    foreach (RadTreeNode node in invalidParents)
    {
        RadTreeNode parent = node;
 
        while (parent != null)
        {
            fieldInfo.SetValue(parent, ToggleState.Indeterminate);
            parent = parent.Parent;
        }
    }
}

You can use the Visual Style Builder tool in order to to change the visual representation of the intermediate state of the check box for a specific theme. In addition, you should change the visual states for mouse over and pressed. You can read more about how you can use Visual Style Builder in the online documentation. Also, attached you can find a screenshot that presents the visual states that you should modify.

I hope this helps. Your Telerik points have been updated for this report.

Regards,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
Treeview
Asked by
Peter Bogoyavlensky
Top achievements
Rank 1
Answers by
Svett
Telerik team
Share this question
or