Home > Software design >  WPF - CommandBinding to a Command defined in a F# library doesn't work
WPF - CommandBinding to a Command defined in a F# library doesn't work

Time:11-16

The repo enter image description here

CodePudding user response:

The problem is that your F# code is creating a new RoutedCommand each time FsAssemblyCommand is invoked. As a result, the corresponding command binding is never found in the element tree.

If you want this member to behave like a C# field that's only initialized once, you can use member val instead:

type CustomCommands() =
    static member val FsAssemblyCommand = RoutedCommand("OpenChartCommand", typeof<CustomCommands>)

Or you can create a let-bound value and return it each time:

type CustomCommands() =
    static let cmd = RoutedCommand("OpenChartCommand", typeof<CustomCommands>)
    static member FsAssemblyCommand = cmd
  • Related