67 people like it.

ObservableObject

The ObservableObject type implements the INotifyPropertyChanged interface used in WPF and Silverlight to notify on changes to properties that are bound to a control. Specify property names type safely using F# Quotations, i.e. <@ this.PropertyName @> when invoking the NotifyPropertyChanged method. If you are following the MVVM pattern then your View Model class can inherit from the ObservableObject type.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
#if INTERACTIVE
#r "PresentationCore.dll"
#r "PresentationFramework.dll"
#r "WindowsBase.dll"
#endif

open System.ComponentModel
open Microsoft.FSharp.Quotations.Patterns

type ObservableObject () =
    let propertyChanged = 
        Event<PropertyChangedEventHandler,PropertyChangedEventArgs>()
    let getPropertyName = function 
        | PropertyGet(_,pi,_) -> pi.Name
        | _ -> invalidOp "Expecting property getter expression"
    interface INotifyPropertyChanged with
        [<CLIEvent>]
        member this.PropertyChanged = propertyChanged.Publish
    member this.NotifyPropertyChanged propertyName = 
        propertyChanged.Trigger(this,PropertyChangedEventArgs(propertyName))
    member this.NotifyPropertyChanged quotation = 
        quotation |> getPropertyName |> this.NotifyPropertyChanged

type MessageViewModel () =
    inherit ObservableObject()
    let mutable text = ""
    member this.Message
        with get () = text
        and set value = 
            text <- value
            this.NotifyPropertyChanged <@ this.Message @>

open System.Windows
open System.Windows.Controls
open System.Windows.Markup

let xaml = 
    @"<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
              xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
       <TextBlock Text='{Binding Message}'/>
      </Window>"
let view = XamlReader.Parse(xaml) :?> Window
do  view.DataContext <- MessageViewModel(Message="Hello World")
#if INTERACTIVE
do  view.Show() |> ignore
#else
[<System.STAThread>]
do  (Application()).Run(view) |> ignore
#endif
namespace System
namespace System.ComponentModel
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
module Patterns

from Microsoft.FSharp.Quotations
Multiple items
type ObservableObject =
  interface INotifyPropertyChanged
  new : unit -> ObservableObject
  member NotifyPropertyChanged : propertyName:string -> unit
  member NotifyPropertyChanged : quotation:Expr -> unit

Full name: Script.ObservableObject

--------------------
new : unit -> ObservableObject
val propertyChanged : Event<PropertyChangedEventHandler,PropertyChangedEventArgs>
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
type PropertyChangedEventHandler =
  delegate of obj * PropertyChangedEventArgs -> unit

Full name: System.ComponentModel.PropertyChangedEventHandler
Multiple items
type PropertyChangedEventArgs =
  inherit EventArgs
  new : propertyName:string -> PropertyChangedEventArgs
  member PropertyName : string

Full name: System.ComponentModel.PropertyChangedEventArgs

--------------------
PropertyChangedEventArgs(propertyName: string) : unit
val getPropertyName : (Quotations.Expr -> string)
active recognizer PropertyGet: Quotations.Expr -> (Quotations.Expr option * System.Reflection.PropertyInfo * Quotations.Expr list) option

Full name: Microsoft.FSharp.Quotations.Patterns.( |PropertyGet|_| )
val pi : System.Reflection.PropertyInfo
property System.Reflection.MemberInfo.Name: string
val invalidOp : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.invalidOp
type INotifyPropertyChanged =
  event PropertyChanged : PropertyChangedEventHandler

Full name: System.ComponentModel.INotifyPropertyChanged
Multiple items
type CLIEventAttribute =
  inherit Attribute
  new : unit -> CLIEventAttribute

Full name: Microsoft.FSharp.Core.CLIEventAttribute

--------------------
new : unit -> CLIEventAttribute
val this : ObservableObject
override ObservableObject.PropertyChanged : IEvent<PropertyChangedEventHandler,PropertyChangedEventArgs>

Full name: Script.ObservableObject.PropertyChanged
property Event.Publish: IEvent<PropertyChangedEventHandler,PropertyChangedEventArgs>
member ObservableObject.NotifyPropertyChanged : propertyName:string -> unit

Full name: Script.ObservableObject.NotifyPropertyChanged
val propertyName : string
member Event.Trigger : sender:obj * args:'Args -> unit
member ObservableObject.NotifyPropertyChanged : quotation:Quotations.Expr -> unit

Full name: Script.ObservableObject.NotifyPropertyChanged
val quotation : Quotations.Expr
member ObservableObject.NotifyPropertyChanged : propertyName:string -> unit
member ObservableObject.NotifyPropertyChanged : quotation:Quotations.Expr -> unit
Multiple items
type MessageViewModel =
  inherit ObservableObject
  new : unit -> MessageViewModel
  member Message : string
  member Message : string with set

Full name: Script.MessageViewModel

--------------------
new : unit -> MessageViewModel
val mutable text : string
val this : MessageViewModel
member MessageViewModel.Message : string with set

Full name: Script.MessageViewModel.Message
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val value : string
property MessageViewModel.Message: string
namespace System.Windows
namespace System.Windows.Controls
namespace System.Windows.Markup
val xaml : string

Full name: Script.xaml
val view : Window

Full name: Script.view
Multiple items
type XamlReader =
  new : unit -> XamlReader
  member CancelAsync : unit -> unit
  member LoadAsync : stream:Stream -> obj + 2 overloads
  event LoadCompleted : AsyncCompletedEventHandler
  static member GetWpfSchemaContext : unit -> XamlSchemaContext
  static member Load : stream:Stream -> obj + 3 overloads
  static member Parse : xamlText:string -> obj + 1 overload

Full name: System.Windows.Markup.XamlReader

--------------------
XamlReader() : unit
XamlReader.Parse(xamlText: string) : obj
XamlReader.Parse(xamlText: string, parserContext: ParserContext) : obj
Multiple items
type Window =
  inherit ContentControl
  new : unit -> Window
  member Activate : unit -> bool
  member AllowsTransparency : bool with get, set
  member Close : unit -> unit
  member DialogResult : Nullable<bool> with get, set
  member DragMove : unit -> unit
  member Hide : unit -> unit
  member Icon : ImageSource with get, set
  member IsActive : bool
  member Left : float with get, set
  ...

Full name: System.Windows.Window

--------------------
Window() : unit
property FrameworkElement.DataContext: obj
Multiple items
type STAThreadAttribute =
  inherit Attribute
  new : unit -> STAThreadAttribute

Full name: System.STAThreadAttribute

--------------------
System.STAThreadAttribute() : unit
Multiple items
active recognizer Application: Quotations.Expr -> (Quotations.Expr * Quotations.Expr) option

Full name: Microsoft.FSharp.Quotations.Patterns.( |Application|_| )

--------------------
type Application =
  inherit DispatcherObject
  new : unit -> Application
  member FindResource : resourceKey:obj -> obj
  member MainWindow : Window with get, set
  member Properties : IDictionary
  member Resources : ResourceDictionary with get, set
  member Run : unit -> int + 1 overload
  member Shutdown : unit -> unit + 1 overload
  member ShutdownMode : ShutdownMode with get, set
  member StartupUri : Uri with get, set
  member TryFindResource : resourceKey:obj -> obj
  ...

Full name: System.Windows.Application

--------------------
Application() : unit
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore

More information

Link:http://fssnip.net/2x
Posted:13 years ago
Author:Phillip Trelford
Tags: wpf , silverlight , mvvm