Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port SelectingItemsBehavior to DataGrid #394

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading