WPF Event to Command

This snippet provides an example of a WPF Event to Command behavior.

Copy Source
Copy Link
Tools:
 1: open System
 2: open System.Windows
 3: open System.Windows.Input
 4: open System.Windows.Interactivity
 5: 
 6: type EventToCommand() =
 7:     inherit TriggerAction<DependencyObject>()
 8:     [<DefaultValue(false)>] static val mutable private CommandProperty:DependencyProperty
 9:     [<DefaultValue(false)>] static val mutable private CommandParameterProperty:DependencyProperty
10:     
11:     /// Set the command dependency property
12:     static do 
13:         EventToCommand.CommandProperty <-
14:             DependencyProperty.Register("Command", typeof<ICommand>, typeof<EventToCommand>)
15:     
16:     /// Set the command parameter dependency property
17:     static do 
18:         EventToCommand.CommandParameterProperty <-
19:             DependencyProperty.Register("CommandParameter", typeof<obj>, typeof<EventToCommand>)
20:     
21:     /// Get/Set the Command 
22:     member this.Command 
23:         with get() = this.GetValue EventToCommand.CommandProperty :?> ICommand
24:         and set value = this.SetValue(EventToCommand.CommandProperty, value)
25:     
26:     /// Get/Set the CommandParameter 
27:     member this.CommandParameter 
28:         with get() = this.GetValue EventToCommand.CommandParameterProperty 
29:         and set value = this.SetValue(EventToCommand.CommandParameterProperty, value)
30:     
31:     /// Implement the Invoke method from TriggerAction to execute the command
32:     override this.Invoke parameter = 
33:         let command = this.Command
34:         let commandParameter = match this.CommandParameter with
35:                                | null -> parameter
36:                                | commandParam -> commandParam  
37:         if command <> null && command.CanExecute(commandParameter) then
38:             command.Execute(commandParameter)
namespace System
namespace System.Windows
namespace System.Windows.Input
type EventToCommand =
  class
    inherit obj
    new : unit -> EventToCommand
    override Invoke : parameter:'a -> 'b
    member Command : 'a
    member CommandParameter : 'a
    member Command : 'a with set
    member CommandParameter : 'a with set
    static val mutable private CommandProperty: DependencyProperty
    static val mutable private CommandParameterProperty: DependencyProperty
  end

Full name: Snippet.EventToCommand
type TriggerAction =
  class
    inherit System.Windows.DependencyObject
  end

Full name: System.Windows.TriggerAction

  type: TriggerAction
  inherits: DependencyObject
  inherits: Threading.DispatcherObject
type DependencyObject =
  class
    inherit System.Windows.Threading.DispatcherObject
    new : unit -> System.Windows.DependencyObject
    member ClearValue : System.Windows.DependencyProperty -> unit
    member ClearValue : System.Windows.DependencyPropertyKey -> unit
    member CoerceValue : System.Windows.DependencyProperty -> unit
    member DependencyObjectType : System.Windows.DependencyObjectType
    member Equals : obj -> bool
    member GetHashCode : unit -> int
    member GetLocalValueEnumerator : unit -> System.Windows.LocalValueEnumerator
    member GetValue : System.Windows.DependencyProperty -> obj
    member InvalidateProperty : System.Windows.DependencyProperty -> unit
    member IsSealed : bool
    member ReadLocalValue : System.Windows.DependencyProperty -> obj
    member SetCurrentValue : System.Windows.DependencyProperty * obj -> unit
    member SetValue : System.Windows.DependencyProperty * obj -> unit
    member SetValue : System.Windows.DependencyPropertyKey * obj -> unit
  end

Full name: System.Windows.DependencyObject

  type: DependencyObject
  inherits: Threading.DispatcherObject
type DefaultValueAttribute =
  class
    inherit Attribute
    new : unit -> DefaultValueAttribute
    new : check:bool -> DefaultValueAttribute
    member Check : bool
  end

Full name: Microsoft.FSharp.Core.DefaultValueAttribute

  type: DefaultValueAttribute
  implements: Runtime.InteropServices._Attribute
  inherits: Attribute
EventToCommand.CommandProperty: DependencyProperty
type DependencyProperty =
  class
    member AddOwner : System.Type -> System.Windows.DependencyProperty
    member AddOwner : System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
    member DefaultMetadata : System.Windows.PropertyMetadata
    member GetHashCode : unit -> int
    member GetMetadata : System.Type -> System.Windows.PropertyMetadata
    member GetMetadata : System.Windows.DependencyObject -> System.Windows.PropertyMetadata
    member GetMetadata : System.Windows.DependencyObjectType -> System.Windows.PropertyMetadata
    member GlobalIndex : int
    member IsValidType : obj -> bool
    member IsValidValue : obj -> bool
    member Name : string
    member OverrideMetadata : System.Type * System.Windows.PropertyMetadata -> unit
    member OverrideMetadata : System.Type * System.Windows.PropertyMetadata * System.Windows.DependencyPropertyKey -> unit
    member OwnerType : System.Type
    member PropertyType : System.Type
    member ReadOnly : bool
    member ToString : unit -> string
    member ValidateValueCallback : System.Windows.ValidateValueCallback
    static val UnsetValue : obj
    static member Register : string * System.Type * System.Type -> System.Windows.DependencyProperty
    static member Register : string * System.Type * System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
    static member Register : string * System.Type * System.Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyProperty
    static member RegisterAttached : string * System.Type * System.Type -> System.Windows.DependencyProperty
    static member RegisterAttached : string * System.Type * System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
    static member RegisterAttached : string * System.Type * System.Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyProperty
    static member RegisterAttachedReadOnly : string * System.Type * System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyPropertyKey
    static member RegisterAttachedReadOnly : string * System.Type * System.Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyPropertyKey
    static member RegisterReadOnly : string * System.Type * System.Type * System.Windows.PropertyMetadata -> System.Windows.DependencyPropertyKey
    static member RegisterReadOnly : string * System.Type * System.Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyPropertyKey
  end

Full name: System.Windows.DependencyProperty
EventToCommand.CommandParameterProperty: DependencyProperty
Multiple overloads
DependencyProperty.Register(name: string, propertyType: Type, ownerType: Type) : DependencyProperty
DependencyProperty.Register(name: string, propertyType: Type, ownerType: Type, typeMetadata: PropertyMetadata) : DependencyProperty
DependencyProperty.Register(name: string, propertyType: Type, ownerType: Type, typeMetadata: PropertyMetadata, validateValueCallback: ValidateValueCallback) : DependencyProperty
val typeof<'T> : Type

Full name: Microsoft.FSharp.Core.Operators.typeof
Multiple items
type ICommand =
  interface
    member CanExecute : obj -> bool
    member Execute : obj -> unit
  end

Full name: System.Windows.Input.ICommand

--------------------

ICommand
type obj = Object

Full name: Microsoft.FSharp.Core.obj
val set : seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
override EventToCommand.Invoke : parameter:'a -> 'b

Full name: Snippet.EventToCommand.Invoke

Implement the Invoke method from TriggerAction to execute the command

More information

Link: http://fssnip.net/6h
Posted: 7 months ago
Author: Daniel Mohl (website)
Tags: WPF