From 2b69464f9b68d2c940db17879ca43993b86c1f06 Mon Sep 17 00:00:00 2001 From: Joshua Marner Date: Mon, 22 Apr 2024 17:28:12 -0500 Subject: [PATCH 1/2] Add typed version of TwoWay --- src/Elmish.WPF/Binding.fs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Elmish.WPF/Binding.fs b/src/Elmish.WPF/Binding.fs index 33d17008..d3c07694 100644 --- a/src/Elmish.WPF/Binding.fs +++ b/src/Elmish.WPF/Binding.fs @@ -116,6 +116,16 @@ module Binding = OneWayToSource.id |> createBindingT + /// + /// Strongly-typed bindings that update both ways + /// + module TwoWayT = + + /// Elemental instance of a two-way binding. + let id<'a> : string -> Binding<'a, 'a, 'a> = + TwoWay.id + |> createBindingT + /// /// Strongly-typed bindings that dispatch messages from the view. /// From 229fd2c81250ccc182163059d9c097bb7deead49 Mon Sep 17 00:00:00 2001 From: Joshua Marner Date: Mon, 22 Apr 2024 17:28:35 -0500 Subject: [PATCH 2/2] Add example of TwoWayT --- src/Samples/SubModelStatic.Core/Program.fs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Samples/SubModelStatic.Core/Program.fs b/src/Samples/SubModelStatic.Core/Program.fs index 70f29573..297e7d08 100644 --- a/src/Samples/SubModelStatic.Core/Program.fs +++ b/src/Samples/SubModelStatic.Core/Program.fs @@ -35,11 +35,17 @@ module Counter = type [] CounterViewModel (args) = inherit ViewModelBase(args) + let stepSizeBinding = + Binding.TwoWayT.id + >> Binding.addLazy (=) + >> Binding.mapModel (fun (m: Counter.Model) -> m.StepSize) + >> Binding.mapMsg Counter.Msg.SetStepSize + new() = CounterViewModel(Counter.init |> ViewModelArgs.simple) member _.StepSize - with get() = base.Get() (Binding.OneWayT.id >> Binding.addLazy (=) >> Binding.mapModel (fun m -> m.StepSize)) - and set(v) = base.Set(v) (Binding.OneWayToSourceT.id >> Binding.mapMsg Counter.Msg.SetStepSize) + with get() = base.Get() stepSizeBinding + and set(v) = base.Set(v) stepSizeBinding member _.CounterValue = base.Get() (Binding.OneWayT.id >> Binding.addLazy (=) >> Binding.mapModel (fun m -> m.Count)) member _.Increment = base.Get() (Binding.CmdT.setAlways Counter.Increment) member _.Decrement = base.Get() (Binding.CmdT.setAlways Counter.Decrement)