From 31e5f42ed8d06c81a0df0fde721e9efb8331f5f9 Mon Sep 17 00:00:00 2001 From: Tim Rudat Date: Tue, 30 Jan 2024 23:25:33 +0100 Subject: [PATCH] Port SelectingItemsBehavior to DataGrid Adds the following functions to the DataGrid control: * selectedIndex * onSelectedIndexChanged * selectedItem * selection * onSelectedItemChanged * onSelectionChanged --- src/Avalonia.FuncUI/DSL/DataGrid.fs | 17 +++++++++++++++-- .../Examples.DataGridPlayground/Program.fs | 14 +++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.FuncUI/DSL/DataGrid.fs b/src/Avalonia.FuncUI/DSL/DataGrid.fs index 83fe1ba1..43343a72 100644 --- a/src/Avalonia.FuncUI/DSL/DataGrid.fs +++ b/src/Avalonia.FuncUI/DSL/DataGrid.fs @@ -1,7 +1,5 @@ namespace Avalonia.FuncUI.DSL -open Avalonia -open Avalonia.Controls.Documents open Avalonia.Controls.Templates open Avalonia.Data open Avalonia.Media @@ -36,6 +34,21 @@ module DataGrid = static member canUserSortColumns<'t when 't :> DataGrid>(value: bool) : IAttr<'t> = AttrBuilder<'t>.CreateProperty(DataGrid.CanUserSortColumnsProperty, value, ValueNone) + + static member selectedIndex<'t when 't :> DataGrid>(index: int) : IAttr<'t> = + AttrBuilder<'t>.CreateProperty(DataGrid.SelectedIndexProperty, index, ValueNone) + + static member selectedItem<'t when 't :> DataGrid>(item: obj) : IAttr<'t> = + AttrBuilder<'t>.CreateProperty(DataGrid.SelectedItemProperty, item, ValueNone) + + static member selection<'t when 't :> DataGrid>(model: DataGridSelectionMode) : IAttr<'t> = + AttrBuilder<'t>.CreateProperty(DataGrid.SelectionModeProperty, model, ValueNone) + + static member onSelectedItemChanged<'t when 't :> DataGrid>(func: obj -> unit, ?subPatchOptions) = + AttrBuilder<'t>.CreateSubscription(DataGrid.SelectedItemProperty, func, ?subPatchOptions = subPatchOptions) + + static member onSelectionChanged<'t when 't :> DataGrid>(func: SelectionChangedEventArgs -> unit, ?subPatchOptions) = + AttrBuilder<'t>.CreateSubscription(DataGrid.SelectionChangedEvent, func, ?subPatchOptions = subPatchOptions) static member columns (columns: IView list) : IAttr<'t> = AttrBuilder<'t>.CreateContentMultiple( diff --git a/src/Examples/Examples.DataGridPlayground/Program.fs b/src/Examples/Examples.DataGridPlayground/Program.fs index d79bf408..4534ae89 100644 --- a/src/Examples/Examples.DataGridPlayground/Program.fs +++ b/src/Examples/Examples.DataGridPlayground/Program.fs @@ -28,14 +28,26 @@ type Views = Person("Bob", 22, true) ] ) + + let selectedItem = ctx.useState None DockPanel.create [ DockPanel.children [ - + TextBlock.create [ + TextBlock.dock Dock.Top + TextBlock.margin 10 + TextBlock.text $"""Selected: {(selectedItem.Current |> Option.defaultValue (Person("", 0, false))).Name}""" + ] DataGrid.create [ DataGrid.dock Dock.Top DataGrid.isReadOnly false DataGrid.items data.Current + DataGrid.onSelectedItemChanged (fun item -> + (match box item with + | null -> None + | :? Person as i -> Some i + | _ -> failwith "Something went horribly wrong!") + |> selectedItem.Set) DataGrid.columns [ DataGridTextColumn.create [