Back to Devexpress

TreeListView.NodeExpanding Event

wpf-devexpress-dot-xpf-dot-grid-dot-treelistview-e575514a.md

latest13.7 KB
Original Source

TreeListView.NodeExpanding Event

Occurs before a node is expanded.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event TreeListNodeAllowEventHandler NodeExpanding
vb
Public Event NodeExpanding As TreeListNodeAllowEventHandler

Event Data

The NodeExpanding event's data class is TreeListNodeAllowEventArgs. The following properties provide information specific to this event:

PropertyDescription
AllowGets or sets whether the operation is allowed.
HandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
NodeGets the processed node. Inherited from TreeListNodeEventArgs.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
RowGets the processed row. Inherited from TreeListNodeEventArgs.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.

The event data class exposes the following methods:

MethodDescription
InvokeEventHandler(Delegate, Object)When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object)When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

Handle this event to:

  • Cancel the action by setting the event parameter’s TreeListNodeAllowEventArgs.Allow property to false ;
  • Dynamically create child nodes. In this instance, when expanding a node, you do not know whether it has child nodes or not. If the node has no child nodes, hide the expand button by setting the TreeListNode.IsExpandButtonVisible property to false.

After a node has been expanded, the TreeListView.NodeExpanded event is raised.

To learn more, see Expanding and Collapsing Nodes.

Example

In this example, the TreeListView displays the file/folder tree. The TreeListView.NodeExpanding event allows you to create child nodes dynamically when a user expands a parent node.

View Example: Load Nodes Dynamically

xaml
<dxg:GridControl x:Name="grid">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="Name"/>
        <dxg:GridColumn FieldName="ItemType"/>
        <dxg:GridColumn FieldName="Size">
            <dxg:GridColumn.EditSettings>
                <dxe:TextEditSettings HorizontalContentAlignment="Right"/>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn FieldName="FullName"/>
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TreeListView x:Name="view" 
                          AllowEditing="False"
                          AutoWidth="True"
                          NodeExpanding="OnNodeExpanding"/>
    </dxg:GridControl.View>
</dxg:GridControl>
cs
public partial class MainWindow : Window {
    FileSystemDataProvider Helper { get; set; }
    public MainWindow() {
        InitializeComponent();
        Helper = new FileSystemHelper();
        InitDrives();
    }
    public void InitDrives() {
        grid.BeginDataUpdate();
        try {
            string[] root = Helper.GetLogicalDrives();

            foreach (string s in root) {
                TreeListNode node = new TreeListNode() { Content = new FileSystemItem(s, "Drive", "<Drive>", s) };
                view.Nodes.Add(node);
                node.IsExpandButtonVisible = DefaultBoolean.True;
            }
        }
        catch { }
        grid.EndDataUpdate();
    }

    private void OnNodeExpanding(object sender, DevExpress.Xpf.Grid.TreeList.TreeListNodeAllowEventArgs e) {
        TreeListNode node = e.Node;
        if (node.Tag == null || (bool)node.Tag == false) {
            InitFolder(node);
            node.Tag = true;
        }
    }

    private void InitFolder(TreeListNode treeListNode) {
        grid.BeginDataUpdate();
        InitFolders(treeListNode);
        InitFiles(treeListNode);
        grid.EndDataUpdate();
    }

    private void InitFolders(TreeListNode treeListNode) {
        FileSystemItem item = treeListNode.Content as FileSystemItem;
        if (item == null) return;

        try {
            string[] root = Helper.GetDirectories(item.FullName);
            foreach (string s in root) {
                try {
                    TreeListNode node = new TreeListNode() { Content = new FileSystemItem(Helper.GetDirectoryName(s), "Folder", "<Folder>", s) };
                    treeListNode.Nodes.Add(node);

                    node.IsExpandButtonVisible = HasFiles(s) ? DefaultBoolean.True : DefaultBoolean.False;
                }
                catch { }
            }
        }
        catch { }
    }

    private void InitFiles(TreeListNode treeListNode) {
        FileSystemItem item = treeListNode.Content as FileSystemItem;
        if (item == null) return;
        TreeListNode node;
        try {
            string[] root = Helper.GetFiles(item.FullName);
            foreach (string s in root) {
                node = new TreeListNode() { Content = new FileSystemItem(Helper.GetFileName(s), "File", Helper.GetFileSize(s).ToString(), s) };
                node.IsExpandButtonVisible = DefaultBoolean.False;
                treeListNode.Nodes.Add(node);
            }
        }
        catch { }
    }

    private bool HasFiles(string path) {
        string[] root = Helper.GetFiles(path);
        if (root.Length > 0) return true;
        root = Helper.GetDirectories(path);
        if (root.Length > 0) return true;
        return false;
    }
}
vb
Public Partial Class MainWindow
    Inherits Window

    Private Property Helper As FileSystemDataProvider

    Public Sub New()
        Me.InitializeComponent()
        Helper = New FileSystemHelper()
        InitDrives()
    End Sub

    Public Sub InitDrives()
        Me.grid.BeginDataUpdate()
        Try
            Dim root As String() = Helper.GetLogicalDrives()
            For Each s As String In root
                Dim node As TreeListNode = New TreeListNode() With {.Content = New FileSystemItem(s, "Drive", "<Drive>", s)}
                Me.view.Nodes.Add(node)
                node.IsExpandButtonVisible = DefaultBoolean.True
            Next
        Catch
        End Try

        Me.grid.EndDataUpdate()
    End Sub

    Private Sub OnNodeExpanding(ByVal sender As Object, ByVal e As TreeList.TreeListNodeAllowEventArgs)
        Dim node As TreeListNode = e.Node
        If node.Tag Is Nothing OrElse CBool(node.Tag) = False Then
            InitFolder(node)
            node.Tag = True
        End If
    End Sub

    Private Sub InitFolder(ByVal treeListNode As TreeListNode)
        Me.grid.BeginDataUpdate()
        InitFolders(treeListNode)
        InitFiles(treeListNode)
        Me.grid.EndDataUpdate()
    End Sub

    Private Sub InitFolders(ByVal treeListNode As TreeListNode)
        Dim item As FileSystemItem = TryCast(treeListNode.Content, FileSystemItem)
        If item Is Nothing Then Return
        Try
            Dim root As String() = Helper.GetDirectories(item.FullName)
            For Each s As String In root
                Try
                    Dim node As TreeListNode = New TreeListNode() With {.Content = New FileSystemItem(Helper.GetDirectoryName(s), "Folder", "<Folder>", s)}
                    treeListNode.Nodes.Add(node)
                    node.IsExpandButtonVisible = If(HasFiles(s), DefaultBoolean.True, DefaultBoolean.False)
                Catch
                End Try
            Next
        Catch
        End Try
    End Sub

    Private Sub InitFiles(ByVal treeListNode As TreeListNode)
        Dim item As FileSystemItem = TryCast(treeListNode.Content, FileSystemItem)
        If item Is Nothing Then Return
        Dim node As TreeListNode
        Try
            Dim root As String() = Helper.GetFiles(item.FullName)
            For Each s As String In root
                node = New TreeListNode() With {.Content = New FileSystemItem(Helper.GetFileName(s), "File", Helper.GetFileSize(s).ToString(), s)}
                node.IsExpandButtonVisible = DefaultBoolean.False
                treeListNode.Nodes.Add(node)
            Next
        Catch
        End Try
    End Sub

    Private Function HasFiles(ByVal path As String) As Boolean
        Dim root As String() = Helper.GetFiles(path)
        If root.Length > 0 Then Return True
        root = Helper.GetDirectories(path)
        If root.Length > 0 Then Return True
        Return False
    End Function
End Class

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the NodeExpanding event.

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.

wpf-treelist-load-nodes-dynamically/CS/DynamicNodeLoading/MainWindow.xaml#L23

xml
AutoWidth="True"
                      NodeExpanding="OnNodeExpanding"/>
</dxg:GridControl.View>

wpf-treelist-load-nodes-dynamically/CS/DynamicNodeLoading/obj/Debug/net8.0-windows/MainWindow.g.cs#L113

csharp
#line 23 "..\..\..\MainWindow.xaml"
this.view.NodeExpanding += new DevExpress.Xpf.Grid.TreeList.TreeListNodeAllowEventHandler(this.OnNodeExpanding);

wpf-treelist-load-nodes-dynamically/VB/DynamicNodeLoading/obj.NetFX/x86/Debug/MainWindow.g.vb#L112

vb
#ExternalSource("..\..\..\MainWindow.xaml",23)
AddHandler Me.view.NodeExpanding, New DevExpress.Xpf.Grid.TreeList.TreeListNodeAllowEventHandler(AddressOf Me.OnNodeExpanding)

See Also

TreeListView Class

TreeListView Members

DevExpress.Xpf.Grid Namespace