Skip to content

Commit

Permalink
Merge pull request #600 from marner2/feature/add_twoWayT
Browse files Browse the repository at this point in the history
Add TwoWayT
  • Loading branch information
marner2 authored Apr 22, 2024
2 parents 7e309e5 + 229fd2c commit ae9a0b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Elmish.WPF/Binding.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ module Binding =
OneWayToSource.id
|> createBindingT

/// <summary>
/// Strongly-typed bindings that update both ways
/// </summary>
module TwoWayT =

/// Elemental instance of a two-way binding.
let id<'a> : string -> Binding<'a, 'a, 'a> =
TwoWay.id
|> createBindingT

/// <summary>
/// Strongly-typed bindings that dispatch messages from the view.
/// </summary>
Expand Down
10 changes: 8 additions & 2 deletions src/Samples/SubModelStatic.Core/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ module Counter =
type [<AllowNullLiteral>] CounterViewModel (args) =
inherit ViewModelBase<Counter.Model, Counter.Msg>(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)
Expand Down

0 comments on commit ae9a0b0

Please sign in to comment.