Skip to main content
A newer version of this page is available. .

TreeListView.NodeExpanding Event

Occurs before a node is expanded.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v19.1.dll

Declaration

public event TreeListNodeAllowEventHandler NodeExpanding

Event Data

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

Property Description
Allow Gets or sets whether the operation is allowed.
Handled Gets 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.
Node Gets the processed node. Inherited from TreeListNodeEventArgs.
OriginalSource Gets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEvent Gets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
Row Gets the processed row. Inherited from TreeListNodeEventArgs.
Source Gets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.

The event data class exposes the following methods:

Method Description
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. Child nodes that correspond to sub folders or files contained within a folder are dynamically created when a parent node is being expanded.

using System;
using System.Windows;
using DevExpress.Xpf.Grid;
using System.IO;
using DevExpress.Utils;

namespace DynamicNodeLoading {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            Helper = new FileSystemHelper();
            InitDrives();
        }

        private void treeListView1_NodeExpanding(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;
            }
        }

        FileSystemDataProvider Helper { get; set; }

        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) };
                    treeListView1.Nodes.Add(node);
                    node.IsExpandButtonVisible = DefaultBoolean.True;
                }
            }
            catch { }
            grid.EndDataUpdate();
        }
        private void InitFolder(TreeListNode treeListNode) {
            grid.BeginDataUpdate();
            InitFolders(treeListNode);
            InitFiles(treeListNode);
            grid.EndDataUpdate();
        }

        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 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 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;
        }
        public abstract class FileSystemDataProvider {
            public abstract string[] GetLogicalDrives();
            public abstract string[] GetDirectories(string path);
            public abstract string[] GetFiles(string path);
            public abstract string GetDirectoryName(string path);
            public abstract string GetFileName(string path);
            public abstract long GetFileSize(string path);
        }
        public class FileSystemHelper : FileSystemDataProvider {

            public override string[] GetLogicalDrives() {
                return Directory.GetLogicalDrives();
            }

            public override string[] GetDirectories(string path) {
                return Directory.GetDirectories(path);
            }

            public override string[] GetFiles(string path) {
                return Directory.GetFiles(path);
            }

            public override string GetDirectoryName(string path) {
                return new DirectoryInfo(path).Name;
            }

            public override string GetFileName(string path) {
                return new FileInfo(path).Name;
            }

            public override long GetFileSize(string path) {
                return new FileInfo(path).Length;
            }
        }
    }

    public class FileSystemItem {
        public FileSystemItem(string name, string type, string size, string fullName) {
            Name = name;
            ItemType = type;
            Size = size;
            FullName = fullName;
        }
        public string Name { get; set; }
        public string ItemType { get; set; }
        public string Size { get; set; }
        public string FullName { get; set; }
    }
}

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.

See Also