elmish / Elmish.WPF

Static WPF views for elmish programs
Other
421 stars 68 forks source link

How to create static `OneWaySeq` binding? #596

Open xperiandri opened 3 months ago

xperiandri commented 3 months ago

High-level description I want to create a static OneWaySeq binding. How to define it?

Additional Information Elmish.WPF version: 4

TysonMN commented 3 months ago

@marner2?

marner2 commented 3 months ago

@xperiandri Could you expand in more detail what exactly you want to get done? There are a lot of things this could mean. The main one is a XAML {x:Static Class.StaticObject} binding, which we use for design-time view models in our examples. The other main one in the context of this project is the statically-typed bindings.

Preferably, please provide a link to a working example repository that can be viewed publicly for easier discussion. Examples are much easier to talk about.

xperiandri commented 3 months ago

I mean Binding.oneWaySeq inside a statically typed view model:

    member _.User = base.Get () (Binding.OneWayT.id >> Binding.mapModel (fun m -> m.User))
    member _.RecentBuildings = base.Get () (Binding.oneWaySeq ((fun m -> m.RecentBuildings), (=), id))
    member _.RecentAssets = base.Get () (Binding.oneWaySeq ((fun m -> m.RecentAssets), (=), id))

    member _.SearchFilter = base.Get () (Binding.TwoWay.id >> Binding.mapModel (fun m -> m.SearchFilter) >> Binding.mapMsg (fun s -> SearchFilterChanged s))
    member _.FindCommand = base.Get() (Binding.CmdT.setAlways Find)
xperiandri commented 2 months ago

Any ideas? Was this scenario considered?

marner2 commented 2 months ago

Ah, that looks like an oversight on my part when porting everything over. Binding.oneWaySeq under the hood returns an ObservableCollection<'T> when given any seq<'T>. In this case, it gets boxed to an obj before being applied to the member. You do lose the type information for that member, but it's perfectly safe to use those old bindings in a statically typed view model (in fact, the only thing the "T" functions do differently is not box the output of the corresponding non-T ones).

This is an aside, but I noticed your _.SearchFilter member is still using Binding.TwoWay.id. See /src/Samples/SubModelStatic.Core/Program.fs#L40 for an example on how this works with two separate bindings for get and set of two way properties.

xperiandri commented 2 months ago

Actually, I use the port of Elmish.WPF to Uno So I took v4, migrated it from scratch (previously I used v3) and implemented TwoWayT for me as OneWayToSource is not supported in WinUI. Thanks for suggestion