I'm trying to fire commands based on keystrokes in Silverlight. As I understand you cannot use AccessKey or AcceleratorKey in Silverlight. Also it looks like the might be helpful attached property InputBindings does not work either.
I started looking in other places. It looked like Prism was the way to get commands working in Silverlight, so I checked that out. However they only have a Click handler, which isn't even a useful starting point for getting key commands set up.
Am I just missing some part of Prism? Or is there a good standard way of handling hotkeys with MVVM Silverlight?
It sounds like you're looking for a "codeless" MVVMish way of handling the KeyUp/KeyPress/KeyDown event.
Option #1: Prism.
You've mentioned it only ships with the Click command. However, you can add your own attached DPs to enable commands for whatever event you'd like (like KeyUp/KeyDown/KeyPress).
If you're looking for a sample on that Corey has a good one for ToggleButton.Checked/Unchecked events.
http://www.85turns.com/2009/06/24/togglebutton-command-for-prism/
<ToggleButton x:Name="ToggleButton1"
customCommands:Checked.Command="{Binding CheckedCommand}"
customCommands:UnChecked.Command="{Binding UnCheckedCommand}"
Margin="8,8,0,8" Content="Check me"
/>
Also, Erik Mork has an excellent video that gives you a good overview on commands and how to create a custom command Attached DP. http://development-guides.silverbaylabs.org/Video/Prism-Commands
Option #2: Blend Triggers
The Expression Blend SDK ships with Triggers and Behaviours that are spot on to what you're try to do.
Blend Examples codeplex project ships with a EventTrigger you could use:
<i:EventTrigger EventName="Click">
<si:InvokeDataCommand Command="{Binding ShoppingCart.CheckOutCommand}"/>
</i:EventTrigger>
Or, you could create your own custom Trigger for Key stroke events and do there whatever you'd like. Here's a sample:
http://azurecoding.net/blogs/brownie/archive/2009/04/06/blend-behaviors-ftw.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With