Skip to content

Commit e1b0776

Browse files
committed
Add a TreeDataGrid sample
1 parent 047718e commit e1b0776

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

src/Avalonia.FuncUI.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Examples.SettingFocus", "Ex
9292
EndProject
9393
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Avalonia.FuncUI.TreeDataGrid", "Avalonia.FuncUI.TreeDataGrid\Avalonia.FuncUI.TreeDataGrid.fsproj", "{44CEE173-0C6F-F12A-063B-EE64187DC6CD}"
9494
EndProject
95+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Examples.TreeDataGridPlayground", "Examples\Component Examples\Examples.TreeDataGridPlayground\Examples.TreeDataGridPlayground.fsproj", "{D95060A4-0F27-46FE-8D31-C875974EE742}"
96+
EndProject
9597
Global
9698
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9799
Debug|Any CPU = Debug|Any CPU
@@ -218,6 +220,10 @@ Global
218220
{44CEE173-0C6F-F12A-063B-EE64187DC6CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
219221
{44CEE173-0C6F-F12A-063B-EE64187DC6CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
220222
{44CEE173-0C6F-F12A-063B-EE64187DC6CD}.Release|Any CPU.Build.0 = Release|Any CPU
223+
{D95060A4-0F27-46FE-8D31-C875974EE742}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
224+
{D95060A4-0F27-46FE-8D31-C875974EE742}.Debug|Any CPU.Build.0 = Debug|Any CPU
225+
{D95060A4-0F27-46FE-8D31-C875974EE742}.Release|Any CPU.ActiveCfg = Release|Any CPU
226+
{D95060A4-0F27-46FE-8D31-C875974EE742}.Release|Any CPU.Build.0 = Release|Any CPU
221227
EndGlobalSection
222228
GlobalSection(SolutionProperties) = preSolution
223229
HideSolutionNode = FALSE
@@ -255,6 +261,7 @@ Global
255261
{6D2C62FC-5634-4997-AF1F-2E8A5D27E117} = {84811DB3-C276-4F0D-B3BA-78B88E2C6EF0}
256262
{EC63B886-E809-4B74-B533-BFF3D60017C9} = {6D2C62FC-5634-4997-AF1F-2E8A5D27E117}
257263
{2C7F4542-65CE-4EC6-9876-C1C2215EE006} = {F50826CE-D9BC-45CF-A110-C42225B75AD3}
264+
{D95060A4-0F27-46FE-8D31-C875974EE742} = {F50826CE-D9BC-45CF-A110-C42225B75AD3}
258265
EndGlobalSection
259266
GlobalSection(ExtensibilityGlobals) = postSolution
260267
SolutionGuid = {4630E817-6780-4C98-9379-EA3B45224339}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="Program.fs" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
14+
<PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaVersion)" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\..\Avalonia.FuncUI.TreeDataGrid\Avalonia.FuncUI.TreeDataGrid.fsproj" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
namespace Examples.TreeDataGridPlaygrounf
2+
3+
open System.Collections.ObjectModel
4+
open Avalonia
5+
open Avalonia.Controls
6+
open Avalonia.Controls.ApplicationLifetimes
7+
open Avalonia.FuncUI.Hosts
8+
open Avalonia.Themes.Fluent
9+
open Avalonia.FuncUI
10+
open Avalonia.FuncUI.DSL
11+
open Avalonia.Controls.Models.TreeDataGrid
12+
13+
type Person (name, age, male, children) =
14+
member val Name = name with get, set
15+
member val Age = age with get, set
16+
member val IsMale = male with get, set
17+
member val Children = children with get, set
18+
19+
[<AbstractClass; Sealed>]
20+
type Views =
21+
22+
static member main () =
23+
Component (fun ctx ->
24+
let data = ctx.useState (
25+
ObservableCollection [
26+
Person("John", 63, true, [
27+
Person("Bob", 32, true, [])
28+
Person("Jill", 30, false, [
29+
Person("Peter", 12, true, [])
30+
])
31+
])
32+
Person("Jane", 25, false, [
33+
Person("Tim", 6, true, [])
34+
])
35+
Person("Bob", 16, true, [])
36+
]
37+
)
38+
39+
let selectedItem = ctx.useState None
40+
41+
DockPanel.create [
42+
DockPanel.children [
43+
TextBlock.create [
44+
TextBlock.dock Dock.Top
45+
TextBlock.margin 10
46+
TextBlock.text $"""Selected: {(selectedItem.Current |> Option.defaultValue (Person("", 0, false, []))).Name}"""
47+
]
48+
TreeDataGrid.create [
49+
TreeDataGrid.dock Dock.Top
50+
51+
TreeDataGrid.init (fun ctrl ->
52+
let dataSource = new HierarchicalTreeDataGridSource<Person>(data.Current)
53+
54+
dataSource.Columns.Add(
55+
HierarchicalExpanderColumn<Person>(
56+
TextColumn<Person, string>("Name", _.Name),
57+
_.Children
58+
)
59+
)
60+
61+
dataSource.Columns.Add (TextColumn<Person, int>("Age", _.Age))
62+
dataSource.Columns.Add (CheckBoxColumn<Person>("IsMale", _.IsMale, fun o v -> o.IsMale <- v))
63+
64+
dataSource.RowSelection.SelectionChanged.Add (fun (args) ->
65+
(if args.SelectedItems.Count = 0 then
66+
None
67+
else
68+
Some args.SelectedItems[0])
69+
|> selectedItem.Set
70+
)
71+
72+
ctrl.Source <- dataSource
73+
)
74+
]
75+
]
76+
]
77+
)
78+
79+
type MainWindow() as this =
80+
inherit HostWindow()
81+
do
82+
base.Title <- "TreeDataGrid Playground"
83+
base.Width <- 500.0
84+
base.Height <- 500.0
85+
this.Content <- Views.main ()
86+
87+
type App() =
88+
inherit Application()
89+
90+
override this.Initialize() =
91+
this.Styles.Add (FluentTheme())
92+
this.RequestedThemeVariant <- Styling.ThemeVariant.Dark
93+
this.Styles.Load "avares://Avalonia.Controls.TreeDataGrid/Themes/Fluent.axaml"
94+
95+
override this.OnFrameworkInitializationCompleted() =
96+
match this.ApplicationLifetime with
97+
| :? IClassicDesktopStyleApplicationLifetime as desktopLifetime ->
98+
desktopLifetime.MainWindow <- MainWindow()
99+
100+
| _ -> ()
101+
102+
module Program =
103+
104+
[<EntryPoint>]
105+
let main(args: string[]) =
106+
AppBuilder
107+
.Configure<App>()
108+
.UsePlatformDetect()
109+
.UseSkia()
110+
.StartWithClassicDesktopLifetime(args)

0 commit comments

Comments
 (0)