F#/WPF event binding

后端 未结 4 1604
青春惊慌失措
青春惊慌失措 2020-12-29 16:48

I am wandering if there is a way of hooking an event defined in XAML to a F# function of member ? Of course, I could do it diagrammatically but it is kind of inconvenient. <

4条回答
  •  感情败类
    2020-12-29 17:34

    It is possible to add binding to a command, eg. using the Command="..." property in the button.

    So in you XAML you can have:

    Then in your ViewModel code, if you have a member called MyCommandHandler, it'll be bound to the above button. So in your F#, something like:

    module ViewModel =
        type FuncCommand (canExec:(obj -> bool), doExec:(obj -> unit)) =     
            let theEvent = new DelegateEvent()     
            interface ICommand with         
                []         
                member x.CanExecuteChanged = theEvent.Publish         
                member x.CanExecute arg = canExec(arg)         
                member x.Execute arg = doExec(arg)
    
        type MyViewModel() =
            member this.MyCommandHandler =          
                new FuncCommand(             
                    (fun _ -> ... SOME CODE WHICH RETURNS TRUE OR FALSE ...),             
                    (fun _ -> ... SOME CODE TO HANDLE THE CLICK ...)
                )
    

提交回复
热议问题