# Expanding and Collapsing TreeView Items

The `RadTreeView` control exposes methods and commands that allow you to control the expand/collapse state of its item/all items.

The available methods are:

* `Expand(params object[] itemPath)`—Expands the item in hierarchy with the specified path. The `itemPath` parameter specifies the path to an item in the hierarchy to expand. The path is a collection if unique identifiers of items. The first element of the path identifies an item at the root level. The second element of the path identifies a child of the root item etc. The `Telerik.Maui.Controls.TreeView.TreeViewDescriptor.IdentityMemberPath` property has to be used to specify the unique identifier at each level of the hierarchy.
* `Collapse(params object[] itemPath)`—Collapses the item in hierarchy with the specified path. The `itemPath` parameter specifies the path to an item in the hierarchy to collapse. The path is a collection if unique identifiers of items. The first element of the path identifies an item at the root level. The second element of the path identifies a child of the root item etc. The `Telerik.Maui.Controls.TreeView.TreeViewDescriptor.IdentityMemberPath` property has to be used to specify the unique identifier at each level of the hierarchy.
* `ExpandAll()`—Expands all items in the source collection.
* `CollapseAll()`—Collapses all items in the source collection.

The available commands are:

* `ExpandCommand`—Gets a command to expand a specific item in the control. The command accepts a parameter specifying the path to the item. The path is a collection if unique identifiers of items. The first element of the path identifies an item at the root level. The second element of the path identifies a child of the root item etc. The `Telerik.Maui.Controls.TreeView.TreeViewDescriptor.IdentityMemberPath` property has to be used to specify the unique identifier at each level of the hierarchy.
* `CollapseCommand`—Gets a command to collapse a specific item in the control. The command accepts a parameter specifying the path to the item. The path is a collection if unique identifiers of items. The first element of the path identifies an item at the root level. The second element of the path identifies a child of the root item etc. The `Telerik.Maui.Controls.TreeView.TreeViewDescriptor.IdentityMemberPath` property has to be used to specify the unique identifier at each level of the hierarchy.
* `ExpandAllCommand`—Gets a command to expand all items in the control.
* `CollapseAllCommand`—Gets a command to collapse all items in the control.

> On Android and iOS, when tapping on the TreeView item, the item gets expanded. On WinUI and MacCatalyst, the item gets expanded when tapping on the arrow **>**.

![.NET MAUI TreeView Expand and Collapse](https://www.telerik.com/maui-ui/documentation/assets/04ed4c491350ce4515e4b1878e63ae8d/treeview-expand-collapse.gif)

## Example: Expanding an Item Using the Expand Method

Review the following knowledge base article: [Using the Expand Method of TreeView in UI for .NET MAUI](https://www.telerik.com/maui-ui/documentation/knowledge-base/expand-item-method-treeview-dotnet-maui). The example demonstrates how to use the `Expand` method of the TreeView control in UI for .NET MAUI to expand specific nodes programmatically.

## Example: Expanding and Collapsing Item Using the Commands

Review the following knowledge base article: [Using Expand and Collapse Commands in TreeView for .NET MAUI](https://www.telerik.com/maui-ui/documentation/knowledge-base/expand-collapse-item-treeview-dotnet-maui). The example demonstrates how to expand and collapse specific items in the TreeView control for .NET MAUI using commands. The commands take a path as a parameter, entered in an Entry control, and convert it into an item path using a converter.

## Example: Expanding and Collapsing All TreeView Items

**1.** Define the TreeView control:

```XAML
<Grid RowDefinitions="Auto,*"
      RowSpacing="10">
    <HorizontalStackLayout Spacing="10">
        <Button Text="ExpandAll" Clicked="OnExpandAllClicked"/>
        <Button Text="CollapseAll" Clicked="OnCollapseAllClicked"/>
    </HorizontalStackLayout>
    <telerik:RadTreeView x:Name="treeView" Grid.Row="1" ItemsSource="{Binding Items}">
        <telerik:TreeViewDescriptor DisplayMemberPath="Name"
                                    ItemsSourcePath="Children"
                                    TargetType="{x:Type local:Item}" />
        <telerik:RadTreeView.BindingContext>
            <local:ViewModel/>
        </telerik:RadTreeView.BindingContext>
    </telerik:RadTreeView>
</Grid>
```

**2.** Add the `ExpandAll` method called on a button click:

```C#
this.treeView.ExpandAll();
```

**3.** Add the `CollapseAll` method called on a button click:

```C#
this.treeView.CollapseAll();
```

**4.** Add the data model:

```C#
public class Item
{
    public Item(string name)
    {
        this.Name = name;
        this.Children = new ObservableCollection<Item>();
    }

    public string Name { get; set; }
    public IList<Item> Children { get; set; }
}
```

**5.** Add the ViewModel:

```C#
public class ViewModel
{
    public ViewModel()
    {
        Items = new ObservableCollection<Item>();
        Items.Add(new Item("My Projects")
        {
            Children = new List<Item>()
            {
                new Item("Using latest Telerik .NET MAUI controls")
                {
                    Children = new ObservableCollection<Item>()
                    {
                        new Item("TreeView"),
                        new Item("Calendar"),
                        new Item("RichTextEditor"),
                        new Item("PdfViewer"),
                        new Item("SlideView"),
                        new Item("Chat"),
                    }
                },
                new Item("Blank project")
            }
        });
        Items.Add(new Item("Shared Documents")
        {
            Children = new List<Item>()
            {
                new Item("Reports")
                {
                    Children = new List<Item>()
                    {
                        new Item("October"),
                        new Item("November"),
                        new Item("December")
                    }
                }
            }
        });
    }
    public ObservableCollection<Item> Items { get; set; }
}
```

Here is how the TreeView Expand/Collapse All command execution looks:

![.NET MAUI TreeView Expand and Collapse](https://www.telerik.com/maui-ui/documentation/assets/04ed4c491350ce4515e4b1878e63ae8d/treeview-expand-collapse.gif)

> For a runnable example demonstrating the TreeView Expand and Collapse feature, see the [SDKBrowser Demo Application](https://www.telerik.com/maui-ui/documentation/demos-and-sample-apps/sdkbrowser-app) and go to **TreeView > Expand and Collapse or TreeView > Commands**.

## See Also

* [CheckBoxes in TreeView](https://www.telerik.com/maui-ui/documentation/controls/treeview/checkboxes)
* [Styling the TreeView Item](https://www.telerik.com/maui-ui/documentation/controls/treeview/styling/item-style)
* [Scrolling options](https://www.telerik.com/maui-ui/documentation/controls/treeview/scrolling)
* [Multiple and Single Selection](https://www.telerik.com/maui-ui/documentation/controls/treeview/selection)
* [Events](https://www.telerik.com/maui-ui/documentation/controls/treeview/events)
* [Available Commands in .NET MAUI TreeView](https://www.telerik.com/maui-ui/documentation/controls/treeview/commands)
