|
| 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