参考文档1
https://docs.avaloniaui.net/docs/reference/controls/detailed-reference/treedatagrid/
参考文档2,英文版
https://avaloniachina.gitbook.io/avalonia/docs/controls/treedatagrid/creating-a-hierarchical-treedatagrid
参考文档3,中文版
https://pwangs-organization.gitbook.io/avaloniachinese/docs/controls/treedatagrid/creating-a-hierarchical-treedatagrid
一,新建一个avalonia MVVM工程AvaloniaAppTreeDataGrid,删掉MainWindow.xaml里的<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/> ,
二,NuGet 安装TreeDataGrid(valonia.Controls.TreeDataGrid)
三,在App.axaml里添加data grid样式
<Application.Styles>
<FluentTheme/>
<StyleInclude
Source="avares://Avalonia.Controls.TreeDataGrid/Themes/Fluent.axaml"/>
</Application.Styles>
四,按照参考文档2,或参考文档3的操作,添加类或修改代码,
以下是修改后的相关代码
1, Person.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AvaloniaAppTreeDataGrid.Models
{
public class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public int Age { get; set; }
public ObservableCollection<Person> Children { get; } = new();
}
}
2,MainWindowViewModel.cs
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using AvaloniaAppTreeDataGrid.Models;
using System.Collections.ObjectModel;
namespace AvaloniaAppTreeDataGrid.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
#pragma warning disable CA1822 // Mark members as static
//public string Greeting => "Welcome to Avalonia!";
private ObservableCollection<Person> _people = new()
{
new Person
{
FirstName = "Eleanor",
LastName = "Pope",
Age = 32,
Children =
{
new Person { FirstName = "Marcel", LastName = "Gutierrez", Age = 4 },
}
},
new Person
{
FirstName = "Jeremy",
LastName = "Navarro",
Age = 74,
Children =
{
new Person
{
FirstName = "Jane",
LastName = "Navarro",
Age = 42 ,
Children =
{
new Person { FirstName = "Lailah ", LastName = "Velazquez", Age = 16 }
}
},
}
},
new Person { FirstName = "Jazmine", LastName = "Schroeder", Age = 52 },
};
public MainWindowViewModel()
{
Source = new HierarchicalTreeDataGridSource<Person>(_people)
{
Columns =
{
new HierarchicalExpanderColumn<Person>(
new TextColumn<Person, string>("First Name", x => x.FirstName),
x => x.Children),
new TextColumn<Person, string>("Last Name", x => x.LastName),
new TextColumn<Person, int>("Age", x => x.Age),
},
};
}
public HierarchicalTreeDataGridSource<Person> Source { get; }
#pragma warning restore CA1822 // Mark members as static
}
}
3,MainWindow.axaml
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:AvaloniaAppTreeDataGrid.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaAppTreeDataGrid.Views.MainWindow"
x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico"
Title="AvaloniaAppTreeDataGrid">
<TreeDataGrid Source="{Binding Source}"/>
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainWindowViewModel/>
</Design.DataContext>
</Window>
4,App.axaml
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaAppTreeDataGrid.App"
xmlns:local="using:AvaloniaAppTreeDataGrid"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>
<Application.Styles>
<FluentTheme />
<StyleInclude
Source="avares://Avalonia.Controls.TreeDataGrid/Themes/Fluent.axaml"/>
</Application.Styles>
</Application>