Expand and Collapse in WinUI TreeView

8 Jul 20269 minutes to read

The TreeView allows you to expand and collapse the nodes either by user interaction on the nodes or by programmatically.

Expand Action Trigger

Nodes can be expanded or collapsed either by tapping the expander view only, or in both the expander view and the content view, by setting the ExpandActionTrigger property.

<treeView:SfTreeView x:Name="treeView" ExpandActionTrigger="Node" />
// Expands by tapping both expander view and content view.
treeView.ExpandActionTrigger = ExpandActionTrigger.Node;

Auto Expand Mode

By default, the TreeView items are in a collapsed state. You can define how the nodes are expanded when the TreeView is loaded by using the AutoExpandMode property.

The AutoExpandMode property is only applicable for bound mode. For unbound mode, you need to set the IsExpanded property to true when creating the nodes to keep them in an expanded state when loading the TreeView.

  • None: All items are collapsed when loaded.
  • RootNodes: Expands only the root item when loaded.
  • AllNodes: Expands all the items when loaded.

Expand or collapse the nodes based on a property of the underlying data object

You can bind the expand state of a node to a bool property in the underlying data object by using the IsExpandedPropertyName property. The TreeView updates the expanded state of a node when the underlying data object property changes, and vice versa.

<treeView:SfTreeView x:Name="treeView"
         ItemsSource="{Binding Folders}">
    <treeView:SfTreeView.HierarchyPropertyDescriptors>        
        <treeView:HierarchyPropertyDescriptor IsExpandedPropertyName="IsExpanded" ChildPropertyName="SubFiles" TargetType="local:FileManager" />
    </treeView:SfTreeView.HierarchyPropertyDescriptors>
</treeView:SfTreeView>
public class FileManager : INotifyPropertyChanged
{
    private string fileName;        
    private ObservableCollection<FileManager> subFiles;
    private bool isExpanded;

    public ObservableCollection<FileManager> SubFiles
    {
        get { return subFiles; }
        set
        {
            subFiles = value;
            RaisedOnPropertyChanged("SubFiles");
        }
    }

    public string ItemName
    {
        get { return fileName; }
        set
        {
            fileName = value;
            RaisedOnPropertyChanged("ItemName");
        }
    }

    public bool IsExpanded
    {
        get { return isExpanded; }
        set
        {
            isExpanded = value;
            RaisedOnPropertyChanged("IsExpanded");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisedOnPropertyChanged(string _PropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(_PropertyName));
        }
    }
}


public class FileManagerViewModel
{
    private ObservableCollection<FileManager> folders;

    public FileManagerViewModel()
    {
        GenerateSource();            
    }

    public ObservableCollection<FileManager> Folders
    {
        get { return folders; }
        set { this.folders = value; }
    }

    private void GenerateSource()
    {
        var fileManager = new ObservableCollection<FileManager>();
        
        var doc = new FileManager() { ItemName = "Documents", IsExpanded = true };
        var download = new FileManager() { ItemName = "Downloads",  IsExpanded = false };
        
        var pollution = new FileManager() { ItemName = "Environmental Pollution.docx"};
        var globalWarming = new FileManager() { ItemName = "Global Warming.ppt" };
        var sanitation = new FileManager() { ItemName = "Sanitation.docx"};
        var socialNetwork = new FileManager() { ItemName = "Social Network.pdf" };
        var youthEmpower = new FileManager() { ItemName = "Youth Empowerment.pdf" };

        var games = new FileManager() { ItemName = "Game.exe" };
        var tutorials = new FileManager() { ItemName = "Tutorials.zip" };
        var TypeScript = new FileManager() { ItemName = "TypeScript.7z"};
        var uiGuide = new FileManager() { ItemName = "UI-Guide.pdf"};
        
        doc.SubFiles = new ObservableCollection<FileManager>
        {
            pollution,
            globalWarming,
            sanitation,
            socialNetwork,
            youthEmpower
        };
     
        download.SubFiles = new ObservableCollection<FileManager>
        {
            games,
            tutorials,
            TypeScript,
            uiGuide
        };
    
        fileManager.Add(doc);
        fileManager.Add(download);
     
        folders = fileManager;
    }
}

NOTE

IsExpandedPropertyName property is not supported for unbound mode and it accepts only boolean type property.

Programmatic Expand and Collapse

The TreeView allows programmatic expand and collapse based on the TreeViewNode and level by using following methods.

// Expands all the nodes of root level '0'
treeView.ExpandNodes(0);

// Collapses all the nodes of root level '0'
treeView.CollapseNodes(0);

// Expand a particular node.
treeView.ExpandNode(node);

// Collapse a particular node.
treeView.CollapseNode(node);

Expand and Collapse all the nodes

Expand and collapse all the TreeViewNode programmatically at runtime by using the TreeView.ExpandAll method and TreeView.CollapseAll method.

//Expands all the nodes
treeView.ExpandAll();

//Collapses all the nodes
treeView.CollapseAll();

Expand and Collapse using Keyboard

The TreeView allows you to expand and collapse the nodes by using the right and left arrow keys. To expand a node, press the right arrow key; to collapse a node, press the left arrow key on the focused item.

Events

TreeView exposes following events to handle expanding and collapsing of items.

The expanding and collapsing interactions can be handled with the help of NodeCollapsing and NodeExpanding events and expanded and collapsed interactions can be handled with help of NodeCollapsed and NodeExpanded events.