Back to Devexpress

TreeListNode.HasChildren Property

windowsforms-devexpress-dot-xtratreelist-dot-nodes-dot-treelistnode-1fd5b722.md

latest8.0 KB
Original Source

TreeListNode.HasChildren Property

Gets or sets a value indicating whether a node has children.

Namespace : DevExpress.XtraTreeList.Nodes

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
[Browsable(false)]
[DXCategory("Behavior")]
public virtual bool HasChildren { get; set; }
vb
<Browsable(False)>
<DXCategory("Behavior")>
Public Overridable Property HasChildren As Boolean

Property Value

TypeDescription
Boolean

true if a node has children; otherwise, false.

|

Remarks

When the HasChildren and TreeListOptionsView.ShowButtons properties are set to true , the ‘+’ or ‘-‘ button appears to the left of the node name. The ‘+’ button is displayed when a node is collapsed. To expand a node just click it. The TreeList.BeforeExpand and TreeList.AfterExpand events are generated in this instance.

The ‘-‘ button is displayed when a node has children and is expanded. When a user clicks the ‘-‘ button, the TreeList.BeforeCollapse event is generated, the node collapses and the TreeList.AfterCollapse event is raised.

You can prevent expanding or collapsing a node by setting the BeforeExpandEventArgs.CanExpand and BeforeCollapseEventArgs.CanCollapse properties in the TreeList.BeforeExpand and TreeList.BeforeCollapse event handlers to false.

The ‘+’ button indicates visually that the current node contains children, but it is not necessarily the case that the TreeListNode.Nodes property contains them. For instance, this can be used to display child nodes dynamically. To do so, set the HasChildren property to true and implement adding child nodes in a TreeList.BeforeExpand event handler. Thus, child nodes will be added only by user demand.

Example

The following sample code prohibits parent node dragging. The TreeList.BeforeDragNode event is handled for this purpose. The TreeListNode.HasChildren property is used to identify whether the currently processed node has child nodes.

csharp
using DevExpress.XtraTreeList;

private void treeList1_BeforeDragNode(object sender, BeforeDragNodeEventArgs e) {
   if (e.Node.HasChildren) e.CanDrag = false;
}
vb
Imports DevExpress.XtraTreeList

Private Sub TreeList1_BeforeDragNode(ByVal sender As Object, _
ByVal e As BeforeDragNodeEventArgs) Handles TreeList1.BeforeDragNode
   If e.Node.HasChildren Then e.CanDrag = False
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the HasChildren property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-treelist-dynamic-load-nodes-unbound-mode/CS/TreeList-UnboundMode-ViaBeforeExpandEvent/Form1.cs#L98

csharp
node.StateImageIndex = 0;
node.HasChildren = HasFiles(s);
if (node.HasChildren)

winforms-treelist-create-file-manager-drag-drop-files-folders/CS/FileList/FileListHelper.cs#L101

csharp
node.StateImageIndex = 0;
node.HasChildren = HasFiles(s);
if (node.HasChildren)

winforms-treelist-implement-group-level-style-feature/CS/Q220534/Form1.cs#L39

csharp
if (currentNode == null) return;
if (currentNode.HasChildren)
    rects.Add(currentNode.Level, new Rectangle(bounds.Right - viewInfo.RC.LevelWidth, bounds.Y, viewInfo.RC.LevelWidth, bounds.Height));

winforms-treelist-save-restore-expanded-state-of-nodes/CS/TreeListViewState.cs#L89

csharp
public override void Execute(TreeListNode node) {
    if(node.HasChildren && node.Expanded)
        al.Add(node.GetValue(node.TreeList.KeyFieldName));

winforms-treelist-dynamic-load-nodes-unbound-mode/VB/TreeList-UnboundMode-ViaBeforeExpandEvent/Form1.vb#L85

vb
node.StateImageIndex = 0
node.HasChildren = HasFiles(s)
If node.HasChildren Then node.Tag = True

winforms-treelist-create-file-manager-drag-drop-files-folders/VB/FileList/FileListHelper.vb#L83

vb
node.StateImageIndex = 0
node.HasChildren = HasFiles(s)
If node.HasChildren Then node.Tag = True

winforms-treelist-implement-group-level-style-feature/VB/Q220534/Form1.vb#L38

vb
End If
If currentNode.HasChildren Then
    rects.Add(currentNode.Level, New Rectangle(bounds.Right - viewInfo.RC.LevelWidth, bounds.Y, viewInfo.RC.LevelWidth, bounds.Height))

winforms-treelist-save-restore-expanded-state-of-nodes/VB/TreeListViewState.vb#L97

vb
Public Overrides Sub Execute(ByVal node As TreeListNode)
    If node.HasChildren AndAlso node.Expanded Then
        al.Add(node.GetValue(node.TreeList.KeyFieldName))

See Also

Nodes

BeforeExpand

TreeListNode Class

TreeListNode Members

DevExpress.XtraTreeList.Nodes Namespace