# .NET MAUI TreeView Control Template

The Telerik UI for .NET MAUI TreeView provides a `ControlTemplate` property, which defines the visual appearance of the control. To customize the looks of the TreeView, use the default template and modify it.

The control has a set of elements inside the `ControlTemplate`. These elements are `CheckBox`, `Image`, `Expand/Collapse` indicator, and `Label`. To customize the appearance of these elements, use the `ControlTemplate`.

The following example shows how to use the `ControlTemplate` and change the appearance of the control.

**1.** Define the `RadTreeView` control and the `ControlTemplate`:

```XAML
    <ContentPage.Resources>
        <ResourceDictionary>
            
            <ControlTemplate x:Key="myTemplate">
                <telerik:RadBorder BackgroundColor="LightBlue"
                                       BorderColor="{TemplateBinding BorderColor}"
                                       BorderBrush="{TemplateBinding BorderBrush}"
                                       BorderThickness="{TemplateBinding BorderThickness}"
                                       CornerRadius="{TemplateBinding CornerRadius}"
                                       Padding="{TemplateBinding ContentPadding}">
                    <telerik:TreeViewItemLayout Indentation="{TemplateBinding Indentation}"
                                                Spacing="{TemplateBinding Spacing}">

                        <telerik:TreeViewItemExpandButton Style="{TemplateBinding ExpandButtonStyle}"
                                                              Command="{TemplateBinding ToggleExpandedCommand}"
                                                              TextColor="{TemplateBinding TextColor}"
                                                              IsExpanded="{TemplateBinding IsExpanded}"
                                                              IsLeaf="{TemplateBinding IsLeaf}"
                                                              IsEnabled="{TemplateBinding IsEnabled}"
                                                              IsVisible="{TemplateBinding IsExpandButtonVisible}" />
                        <telerik:TreeViewItemCheckBox Style="{TemplateBinding CheckBoxStyle}"
                                                          IsChecked="{TemplateBinding IsChecked}"
                                                          IsEnabled="{TemplateBinding IsEnabled}"
                                                          IsVisible="{TemplateBinding IsCheckBoxVisible}" />
                     
                        <telerik:TreeViewItemImage Source="{TemplateBinding ImageSource}"
                                                       Style="{TemplateBinding ImageStyle}"
                                                       IsEnabled="{TemplateBinding IsEnabled}"
                                                       IsVisible="{TemplateBinding IsImageVisible}" />
                            
                        <ContentPresenter/>

                            
                        <telerik:TreeViewItemButton Text="MyButton" WidthRequest="100" BackgroundColor="Red" HorizontalOptions="End"/>
                    </telerik:TreeViewItemLayout>
                </telerik:RadBorder>
            </ControlTemplate>
            
            <Style TargetType="telerik:TreeViewItemView" x:Key="myStyle" >
                
                <Setter Property="ControlTemplate" Value="{StaticResource myTemplate}"/>
                <Setter Property="IsImageVisible" Value="True"/>
            </Style>
            
        </ResourceDictionary>
    </ContentPage.Resources>
    <telerik:RadTreeView x:Name="treeView"
                         ItemStyle="{StaticResource myStyle}"
                         ItemsSource="{Binding Items}">
        <telerik:TreeViewDescriptor ItemsSourcePath="Children"
                                DisplayMemberPath="Name"
                                TargetType="{x:Type local:Item}" />
    </telerik:RadTreeView>
```

**2.** Add the `telerik` namespaces:

```XAML
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
```

**3.** Create a sample `Item` class:

```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; }
}
```

**4.** Add the `ViewModel` class:

```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; }
}
```

## See Also

* [Expand and Collapse TreeView Items](https://www.telerik.com/maui-ui/documentation/controls/treeview/expand-collapse)
* [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)
* [Available Commands in .NET MAUI TreeView](https://www.telerik.com/maui-ui/documentation/controls/treeview/commands)
