4 people like it.

WPF Event to Command

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

 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: 
open System
open System.Windows
open System.Windows.Input
open System.Windows.Interactivity

type EventToCommand() =
    inherit TriggerAction<DependencyObject>()
    [<DefaultValue(false)>] static val mutable private CommandProperty:DependencyProperty
    [<DefaultValue(false)>] static val mutable private CommandParameterProperty:DependencyProperty
    
    /// Set the command dependency property
    static do 
        EventToCommand.CommandProperty <-
            DependencyProperty.Register("Command", typeof<ICommand>, typeof<EventToCommand>)
    
    /// Set the command parameter dependency property
    static do 
        EventToCommand.CommandParameterProperty <-
            DependencyProperty.Register("CommandParameter", typeof<obj>, typeof<EventToCommand>)
    
    /// Get/Set the Command 
    member this.Command 
        with get() = this.GetValue EventToCommand.CommandProperty :?> ICommand
        and set value = this.SetValue(EventToCommand.CommandProperty, value)
    
    /// Get/Set the CommandParameter 
    member this.CommandParameter 
        with get() = this.GetValue EventToCommand.CommandParameterProperty 
        and set value = this.SetValue(EventToCommand.CommandParameterProperty, value)
    
    /// Implement the Invoke method from TriggerAction to execute the command
    override this.Invoke parameter = 
        let command = this.Command
        let commandParameter = match this.CommandParameter with
                               | null -> parameter
                               | commandParam -> commandParam  
        if command <> null && command.CanExecute(commandParameter) then
            command.Execute(commandParameter)
namespace System
namespace System.Windows
Multiple items
type EventToCommand =
  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: obj
  static val mutable private CommandParameterProperty: obj

Full name: Script.EventToCommand

--------------------
new : unit -> EventToCommand
Multiple items
type DefaultValueAttribute =
  inherit Attribute
  new : unit -> DefaultValueAttribute
  new : check:bool -> DefaultValueAttribute
  member Check : bool

Full name: Microsoft.FSharp.Core.DefaultValueAttribute

--------------------
new : unit -> DefaultValueAttribute
new : check:bool -> DefaultValueAttribute
EventToCommand.CommandProperty: obj
EventToCommand.CommandParameterProperty: obj
val typeof<'T> : Type

Full name: Microsoft.FSharp.Core.Operators.typeof
type obj = Object

Full name: Microsoft.FSharp.Core.obj
member EventToCommand.Command : 'a with set

Full name: Script.EventToCommand.Command


 Set the command dependency property
 Set the command parameter dependency property
 Get/Set the Command
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
member EventToCommand.CommandParameter : 'a with set

Full name: Script.EventToCommand.CommandParameter


 Get/Set the CommandParameter
override EventToCommand.Invoke : parameter:'a -> 'b

Full name: Script.EventToCommand.Invoke


 Implement the Invoke method from TriggerAction to execute the command

More information

Link:http://fssnip.net/6h
Posted:6 years ago
Author:Daniel Mohl
Tags: wpf