1
votes

I have a standard WPF button that uses cal:Message.Attach to execute a function in the ViewModel. I would like to also have that same function executed if I press the F2 button.

Is there a simple xaml declaration using a Caliburn Micro binding that can enable this?

2

2 Answers

3
votes

Override the Configure() method of your bootstrapper: https://github.com/Caliburn-Micro/Caliburn.Micro/blob/master/samples/scenarios/Scenario.KeyBinding/Bootstrapper.cs

...and try this:

<Button x:Name="TheFunction"
        Content="Click"
        cal:Message.Attach="[Key F2] = [TheFunction];" />

There is a full code sample available in the official repo on GitHub: https://github.com/Caliburn-Micro/Caliburn.Micro/tree/master/samples/scenarios/Scenario.KeyBinding

0
votes

You have to add a handler to the event:

public MainWindow()
{
    InitializeComponent();
    this.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
}

void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if ((e.Key == Key.F2))
    { 
         //DO SOMETHING
    }
}