Binding KeyDown Event Silverlight
Asked Answered
F

1

0

I am attempting to have a key binding cause an event within the view model. I have been search for a while and have not come across any solutions that have worked thus far, unfortunately.

This is what I am basically attempting to implement:

<i:Interaction.Triggers>  
    <i:EventTrigger EventName="createNew">  
       <cal:ActionMessage MethodName="newCustomer" />  
    </i:EventTrigger>  
</i:Interaction.Triggers>

I am wanting a way to provide a "hotkey" to allow the user to implement a newCustomer event within the view model. So far it won't even reach into the view model. If I attach the EventName="KeyDown" it works wonderfully if any key is pressed, but I am attempting to target a single key.

I will add that the code behind in the view model looks like this.

public void createNew(object sender, KeyEventArgs e)
     { if (e.Key == Key.F9)
         {
             addCustomer();  
         } }
Fillet answered 3/10, 2012 at 18:38 Comment(1)
A comprehensive answer https://mcmap.net/q/611285/-caliburn-micro-39-enter-39-key-eventPlayreader
A
0

Not too sure what control you are using, but for example purposes I'll use a text-box... Not entirely sure if this will help even, but I hope it does:

The View:

<TextBox x:Name="MyTextBox" TextAlignment="Left" FontSize="10" Width="240" cal:Message.Attach="[Event KeyUp]=[Action ExecuteAction($executionContext)]"/>

The ViewModel:
* For example purposes I'll use "Enter" as the single key on which you want the actions performed.

public void ExecuteAction(ActionExecutionContext context)
    {
        var eventArgs = (KeyEventArgs)context.EventArgs;
        if (eventArgs.Key != Key.Enter) return; 
        //Execute Actions...
    }

This ought to fire code on each key-press on the control you want, if the key is not equal to whatever you specify, it won't do anything...

If I'm waaay off, maybe I just don't understand the issue correctly :D

Abbevillian answered 4/10, 2012 at 10:3 Comment(3)
Unfortunately that did not work. I have tried the caliburn.micro method for this using cal:Message.Attach="[Event createNew] = [Action newCustomer($this)]" and it did not work unfortunately. Thank you for the effort.Fillet
Hmmm... Okay then... Just out of curiosity, looking at your VM method and caliburn XAML, why don't you use the ActionExecutionContext? Is it too bulky for your taste? Only asking as it's got all (well almost all) information about the event...Abbevillian
I was able to get it going after a couple tweaks. Thanks again.Fillet

© 2022 - 2024 — McMap. All rights reserved.