Skip to content

Instantly share code, notes, and snippets.

@willsam100
Last active August 27, 2018 18:17
Show Gist options
  • Save willsam100/8efbae58ebf7f0c3ef843ae30a2144ff to your computer and use it in GitHub Desktop.
Save willsam100/8efbae58ebf7f0c3ef843ae30a2144ff to your computer and use it in GitHub Desktop.
F# ViewModel Base
namespace YourNamespaceHere
open System
open System.Collections.ObjectModel
open System.ComponentModel
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
type ViewModelBase() =
let propertyChanged = new Event<_, _>()
let toPropName(query : Expr) =
match query with
| PropertyGet(a, b, list) ->
b.Name
| _ -> ""
interface INotifyPropertyChanged with
[<CLIEvent>]
member this.PropertyChanged = propertyChanged.Publish
abstract member OnPropertyChanged: string -> unit
default this.OnPropertyChanged(propertyName : string) =
propertyChanged.Trigger(this, new PropertyChangedEventArgs(propertyName))
member this.OnPropertyChanged(expr : Expr) =
let propName = toPropName(expr)
this.OnPropertyChanged(propName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment