TreeView
TreeView is a component that displays a hierarchical list of items. It's a MAUI layer control with complex logic. It also comes with some pre-built behaviors like hierarchical selection.
Usage
TreeView is included in the UraniumUI.Material.Controls
namespace. You should add it to your XAML like this:
Then you can use it like this:
TreeView doesn't have any visual appearance without data. You should bind some data to see the control.
Data Binding
Prepare a ViewModel that contain a list of a node object. Node means a single item in the tree. It can be a folder or a file. It can be a person or a company. It can be a country or a city. It can be anything. It's up to you. So there is no type limitation or no interface implementation required for your object. It can be a simple object that should have a Children
property that includes list of same type of the object when hierarchical data is required.
A simple example of a node object:
Initialize your ViewModel with some data:
Now you can bind your ViewModel to the TreeView:
Light | Dark |
---|---|
![]() |
![]() |
ItemTemplate
You can customize nodes with ItemTemplate
. It's just like a ListView
or CollectionView
:
Light | Dark |
---|---|
![]() |
![]() |
IsExpandedPropertyName
You can bind expanding property to a property of your node object. It's useful when you want to save the state of the tree. For example, you can save the state of the tree in the local storage and restore it when the user opens the app again. To do this, you should set IsExpandedPropertyName
property of the TreeView to the name of the property that contains the state of the node. For example, if you have a IsExtended
property in your node object, you should set IsExpandedPropertyName
to IsExtended
:
They will be rendered as expanded when treeview is initialized.
IsLeafPropertyName
If the node object contain its children in a different property, you can set IsLeafPropertyName
property of the TreeView to the name of the property that contains the state of the node. Default value is IsLeaf
. If you have a IsLeaf
property in your node object, it'll be automatically binded. If your property that declares leaf state has a different name, you should consider to set IsLeafPropertyName
.
Note: Leaf status is already managed by the TreeView. But when lazy-loading is used, TreeView can't know if the node is a leaf or not. So you should set
IsLeafPropertyName
to the name of the property that contains the state of the node.
ChildrenBinding
If the node object contain its children in a different property, you can set ChildrenBinding
property of the TreeView to the name of the property that contains the children. For example, if you have a SubItems
property in your node object, you should set ChildrenBinding
to SubItems
:
Selection
TreeView supports single selection and multiple selection. You can set SelectionMode
property of the TreeView to Single
or Multiple
to enable selection. Default value is None
. You can bind SelectedItem
or SelectedItems
property of the TreeView to a property in your ViewModel to get the selected item or items.
Single Selection
Light | Dark |
---|---|
![]() |
![]() |
Multiple Selection
Light | Dark |
---|---|
![]() |
![]() |
Hierarchical Selection (CheckBox)
TreeView has a special behavior for CheckBoxes. TreeView supports hierarchical selection. It means that when you select a parent node, all of its children will be selected. When you deselect a parent node, all of its children will be deselected. When some of children is selected, CheckBox will enter semi-selected state. It's useful when you want to select a group of items. For example, you can select all files in a folder.
TreeView provides TreeViewHierarchicalSelectBehavior
that can be used only with CheckBox
when it's directly ItemTemplate of the TreeView.
Customizations
TreeView allows you to customize its appearance in a couple of ways.
Styles
You can customize the appearance of the TreeView with styles. You override the following styles in your Page or App.xaml:
Note that, Make sure
material
namespace of TreeView is added to your file, you can addxmlns:material="http://schemas.enisn-projects.io/dotnet/maui/uraniumui/material"
Spacing
You can change the spacing between the arrow icon and the content with Spacing
property. Default value is 10
.
Light | Dark |
---|---|
![]() |
![]() |
ExpanderTemplate
You can completely customize the expander with ExpanderTemplate
property. It's an arrow by default. You can use any view as an expander. For example, you can use a Switch
to expand and collapse nodes.
You can use following binding properties in the ExpanderTemplate
:
IsExpanded
-true
if the node is expanded, otherwisefalse
.IsLeaf
-true
if the node is a leaf, otherwisefalse
. You can use it to manage visibility of the control.
UseAnimation
Determines whether to use animations when expanding and collapsing nodes. Default value is true
. You may want to disable animations if you want to improve performance while working with huge amount of tree nodes.
Enabled | Disabled |
---|---|
![]() |
![]() |
Lazy-Loading
TreeView supports lazy-loading of children. It means that children will be loaded only when the node is expanded. TreeView executes LoadChildrenCommand
command with node item that is expanded as parameter when the node is expanded. You can set LoadChildrenCommand
property of the TreeView to the command that will be executed when the node is expanded. For example, you can load children from the database when the node is expanded.
Following properies can be used to define a propert lazy-loading behavior:
IsLeafPropertyName
- The name of the property that contains the state of the node. Default value isIsLeaf
.LoadChildrenCommand
- The command that will be executed when the node is expanded.
Example
A file system is a good example of lazy-loading. When you open a folder, you don't want to load all of its children. You want to load children only when the folder is expanded. You can use LoadChildrenCommand
to load children from the database when the folder is expanded.
Dark |
---|
![]() |