[DevExpress Support Team: CLONED FROM T202180: How to to make child nodes non editable or not expandable]
I have another question ?
If I check the checkbox of parent node then it should not expand its childnodes
How to do this ?
How to prevent child nodes from being expanded if their parent node is checked
Answers approved by DevExpress Support
Hello,
From what I gather, you want to prevent child nodes from being expanded if their parent node is checked. Based on the How to add Checkedit in parent node for treelist in c# ? ticket, you must be using an in-place CheckEdit to check a node. In this case, I recommend that you handle the BeforeExpand event and cancel the node expansion if its parent node is checked.
C#private string checkedColumnFieldName = "Is Checked";
private void treeList1_BeforeExpand(object sender, DevExpress.XtraTreeList.BeforeExpandEventArgs e) {
TreeListNode parentNode = GetParentByLevel(e.Node, 0);
if (parentNode == null) {
return;
}
bool isChecked = (bool)parentNode.GetValue(checkedColumnFieldName);
e.CanExpand = !isChecked;
}
private TreeListNode GetParentByLevel(TreeListNode node, int level) {
TreeListNode parentNode = node.ParentNode;
while (parentNode != null) {
if (parentNode.Level == level) {
return parentNode;
}
parentNode = parentNode.ParentNode;
}
return null;
}
Visual Basic 6Private checkedColumnFieldName As String = "Is Checked"
Private Sub treeList1_BeforeExpand(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.BeforeExpandEventArgs) Handles treeList1.BeforeExpand
Dim parentNode As TreeListNode = GetParentByLevel(e.Node, 0)
If parentNode Is Nothing Then
Return
End If
Dim isChecked As Boolean = CBool(parentNode.GetValue(checkedColumnFieldName))
e.CanExpand = Not isChecked
End Sub
Private Function GetParentByLevel(ByVal node As TreeListNode, ByVal level As Integer) As TreeListNode
Dim parentNode As TreeListNode = node.ParentNode
Do While parentNode IsNot Nothing
If parentNode.Level = level Then
Return parentNode
End If
parentNode = parentNode.ParentNode
Loop
Return Nothing
End Function
Please note that TreeList provides a built-in functionality allowing you to show check-boxes within nodes. To enable it, set the TreeListOptionsView.ShowCheckBoxes property to true. By using this feature, the abovementioned solution will be simplified:
C#private void treeList1_BeforeExpand(object sender, DevExpress.XtraTreeList.BeforeExpandEventArgs e) {
TreeListNode parentNode = GetParentByLevel(e.Node, 0);
if(parentNode == null)
return;
e.CanExpand = !parentNode.Checked;
}
C#Private Sub treeList1_BeforeExpand(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.BeforeExpandEventArgs) Handles treeList1.BeforeExpand
Dim parentNode As TreeListNode = GetParentByLevel(e.Node, 0)
If parentNode Is Nothing Then
Return
End If
e.CanExpand = Not parentNode.Checked
End Sub
Private Function GetParentByLevel(ByVal node As TreeListNode, ByVal level As Integer) As TreeListNode
Dim parentNode As TreeListNode = node.ParentNode
Do While parentNode IsNot Nothing
If parentNode.Level = level Then
Return parentNode
End If
parentNode = parentNode.ParentNode
Loop
Return Nothing
End Function
I have attached samples showing both approaches. Please check them and let me know if I can be of further help.