Skip to content

Commit

Permalink
Port SelectingItemsBehavior to DataGrid
Browse files Browse the repository at this point in the history
Adds the following functions to the DataGrid control:
	* selectedIndex
	* onSelectedIndexChanged
	* selectedItem
	* selection
	* onSelectedItemChanged
	* onSelectionChanged
  • Loading branch information
Tim Rudat authored and excpt committed Jan 30, 2024
1 parent f0dfd39 commit 31e5f42
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/Avalonia.FuncUI/DSL/DataGrid.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Avalonia.FuncUI.DSL

open Avalonia
open Avalonia.Controls.Documents
open Avalonia.Controls.Templates
open Avalonia.Data
open Avalonia.Media
Expand Down Expand Up @@ -36,6 +34,21 @@ module DataGrid =

static member canUserSortColumns<'t when 't :> DataGrid>(value: bool) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<bool>(DataGrid.CanUserSortColumnsProperty, value, ValueNone)

static member selectedIndex<'t when 't :> DataGrid>(index: int) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<int>(DataGrid.SelectedIndexProperty, index, ValueNone)

static member selectedItem<'t when 't :> DataGrid>(item: obj) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<obj>(DataGrid.SelectedItemProperty, item, ValueNone)

static member selection<'t when 't :> DataGrid>(model: DataGridSelectionMode) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<DataGridSelectionMode>(DataGrid.SelectionModeProperty, model, ValueNone)

static member onSelectedItemChanged<'t when 't :> DataGrid>(func: obj -> unit, ?subPatchOptions) =
AttrBuilder<'t>.CreateSubscription<obj>(DataGrid.SelectedItemProperty, func, ?subPatchOptions = subPatchOptions)

static member onSelectionChanged<'t when 't :> DataGrid>(func: SelectionChangedEventArgs -> unit, ?subPatchOptions) =
AttrBuilder<'t>.CreateSubscription<SelectionChangedEventArgs>(DataGrid.SelectionChangedEvent, func, ?subPatchOptions = subPatchOptions)

static member columns (columns: IView list) : IAttr<'t> =
AttrBuilder<'t>.CreateContentMultiple(
Expand Down
14 changes: 13 additions & 1 deletion src/Examples/Examples.DataGridPlayground/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down

0 comments on commit 31e5f42

Please sign in to comment.