Funk
Funk

Reputation: 11221

F# Event driven MVVM using WPF

I've been working with Event driven MVVM for a couple of weeks (first time using a design pattern in F#) and I like the idea of separating view and model and also the "functional" controller. But when going through a book on WPF I get the feeling it would be easier if I could adress events directly. Also in some situations I need to get a hold on a control from code behind.

More specific:

Does anybody share this experience or am I still missing something? Is it advisable to go back to FsXaml or polyglot MVVM?

Upvotes: 3

Views: 1171

Answers (1)

Funk
Funk

Reputation: 11221

Turns out the code is actually very easy to extend. Based on demos I was able to turn a textbox into a numeric one. The code that does this is very basic, but my intent was to define a custom event accessor. Which can be done by:

Extend UserControl.xaml header with:

xmlns:fsxaml="http://github.com/fsprojects/FsXaml"   
fsxaml:ViewController.Custom="{x:Type views:CompositionUserControl}"

And replace the original code in UserControl.xaml.fs:

namespace Space.Views

open FsXaml

type UserView = XAML<"View/UserControl.xaml", true>

type CompositionUserControl () =

    member __.ViewModel = Space.ViewModels.UserControlViewModel(Space.Models.Handling.proces)

with

namespace Space.Views

open FsXaml
open System

type UserView = XAML<"View/UserControl.xaml", true>

type CompositionUserControl () =
    inherit UserControlViewController<UserView>()

    let numeric (txt : string) =
        try txt |> int with
        | :? System.FormatException -> 0
        | _ -> 1

    override this.OnLoaded view = 
        view.Box.PreviewTextInput.Add(fun e -> if numeric e.Text = 0 then e.Handled <- true) 

    member __.ViewModel = Space.ViewModels.UserControlViewModel(Space.Models.Handling.proces)

EDIT

Looking back at this post, here's my progress regarding my initial questions:

How to close a window defined as usercontrol in a XAML file

Using an attached property DialogCloser.

It seems there would be less need for buttons (triggering booleans that hold state), with a more automated feel as result, if I could directly adress events

The key here is to learn:

  1. How to truly separate View(Model) from Model
  2. How to use XAML to exploit its full power

Upvotes: 4

Related Questions