Skip to content

Instantly share code, notes, and snippets.

@blacktaxi
Last active December 11, 2015 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blacktaxi/4641562 to your computer and use it in GitHub Desktop.
Save blacktaxi/4641562 to your computer and use it in GitHub Desktop.
Base WPF ViewModel in F#
type SomethingViewModel() as this =
inherit ViewModelBase()
let (<<+) (_ : unit) (e : Microsoft.FSharp.Quotations.Expr) =
this.OnPropertyChanged e
let mutable somePropertyValue = ""
member x.SomeProperty
with get() = somePropertyValue
and set value = (somePropertyValue <- value) <<+ <@ x.SomeProperty @>
open System
open System.ComponentModel
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
type ViewModelBase() =
let propertyChanged = new Event<_, _>()
interface INotifyPropertyChanged with
[<CLIEvent>]
member x.PropertyChanged = propertyChanged.Publish
abstract member OnPropertyChanged: string -> unit
default x.OnPropertyChanged(propertyName : string) =
propertyChanged.Trigger(x, new PropertyChangedEventArgs(propertyName))
member x.OnPropertyChanged(expr : Expr) =
let propName =
match expr with
| PropertyGet(a, b, list) -> b.Name
| _ -> failwith "Not a valid property expression %A" expr
x.OnPropertyChanged(propName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment