Ticket T202616
Visible to All Users

How to prevent child nodes from being expanded if their parent node is checked

created 10 years ago (modified 10 years ago)

[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 ?

Answers approved by DevExpress Support

created 10 years ago

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 6
Private 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.

    Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

    Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.