Expand and Collapse with Xamarin.Android TreeView (SfTreeView)

3 Sep 20202 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 Target

Expanding and Collapsing of nodes can be performed either by tapping the expander view or in both expander view and content view by setting the ExpandActionTarget property.

// Expands by tapping both expander view and content view.
treeView.ExpandActionTarget = ExpandActionTarget.Node;

Auto Expand Mode

By default, the treeview items will be in collapsed state. You can define how the nodes to be expanded while loading the TreeView by using AutoExpandMode property.

The AutoExpandMode property is only applicable for bound mode. For Unbound mode you need to set IsExpanded property to true while creating the nodes, to be in expanded state while loading the TreeView.

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

Programmatic Expand and Collapse

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);

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

Expand and Collapse all the nodes

Expand and Collapse all the TreeViewNode programmatically at runtime by using the SfTreeView.ExpandAll method and SfTreeView.CollapseAll method.

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

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

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.