Перейти к основному содержимому
Версия: 0.10.x


The TreeView is a control that presents hierarchical tree data and allows selection of it.

One example for populating a TreeView can be from a directory on the computer. You can create a TreeView in the MainWindow.axaml file in an Avalonia MVVM project.

<TreeView Items="{Binding Items}" 
Width="400" Height="480"
<TreeDataTemplate ItemsSource="{Binding Subfolders}">
<TextBlock Text="{Binding strNodeText}"/>

In the MainWindowViewModel.cs you can add this code which will recursively look up all the subfolders and populate the TreeView from Items and Subfolders

public ObservableCollection<Node> Items { get; }
public ObservableCollection<Node> SelectedItems { get; }
public string strFolder { get; set; }

public MainWindowViewModel()
strFolder = @"C:\Users\hooty\Desktop"; // EDIT THIS FOR AN EXISTING FOLDER

Items = new ObservableCollection<Node>();

Node rootNode = new Node(strFolder);
rootNode.Subfolders = GetSubfolders(strFolder);


public ObservableCollection<Node> GetSubfolders(string strPath)
ObservableCollection<Node> subfolders = new ObservableCollection<Node>();
string[] subdirs = Directory.GetDirectories(strPath, "*", SearchOption.TopDirectoryOnly);

foreach (string dir in subdirs)
Node thisnode = new Node(dir);

if (Directory.GetDirectories(dir, "*", SearchOption.TopDirectoryOnly).Length > 0)
thisnode.Subfolders = new ObservableCollection<Node>();

thisnode.Subfolders = GetSubfolders(dir);


return subfolders;

public class Node
public ObservableCollection<Node> Subfolders { get; set; }

public string strNodeText { get; }
public string strFullPath { get; }

public Node(string _strFullPath)
strFullPath = _strFullPath;
strNodeText = Path.GetFileName(_strFullPath);

The example project source can be found at avalonia-treeview-test



Source code