Link to event from .xaml file in .fs file

I want to declare an event in a XAML file and then add a handler to fs.

The top of the XAML file will look like (MouseRightButtonDown):

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AboutStack" MouseRightButtonDown="AboutStack_MouseRightButtonDown"
Title="About" SizeToContent="WidthAndHeight">
    <StackPanel>...

      

The F # file contains:

open FsXaml

type MainWindow = XAML<"MainWindow.xaml", true> 
let mainwnd = new MainWindow()
let wnd = mainwnd.Root

      

Is there an easy way to make a link?

+3


source to share


1 answer


Since there is no code, you cannot call the method from XAML, but it must be bound from the outside:

let AboutStack_MouseRightButtonDown args =
    // do whatever you want, return unit

wnd.MouseRightButtonDown.Add(AboutStack_MouseRightButtonDown)

      

or for example



wnd.MouseRightButtonDown.Add(fun _ -> MessageBox.Show("Click!") |> ignore)

      

Another approach would be to use Model (DataContext) and bindings to create commands, for example Visual Studio Template for F # Windows App (WPF, MVVM)

+3


source







All Articles