KeyBindings in UserControl View

Topics: Prism v1
Mar 16, 2009 at 11:28 AM
Hi All,

Has anyone used a keybinding defined in a UserControl view?  I've tried a few things but have so far failed to get the correct setup for this.

Confusingly you can't use a binding on a keybinding ... I have a binding on my presenter class that I use for a button like:

<Button Command="{Binding RefreshFilesListCommand}"/>

And I just wish to combine keyboard gestures / keyboard strokes to the same command ... anyone with any info/ideas it would be really appreciated.

Thanks,
Mar 19, 2009 at 4:40 PM
Edited Mar 19, 2009 at 4:49 PM

Hi,

 

Key input is necessarily focus-based. That means that if your control has an input binding you need to make sure that the control is focused in order for the binding to work. In the particular case of a Button, placing your mouse over the button is not enough, you need to press the button in order for the button to gain focus. That is why buttons are not the best choice to put keyBindings. In most scenarios KeyBindings are placed in the Window element or in a control that you are sure it's focused when you need it.

You might want to take a look at Commanding Overview and Input Overview

 

 

To assign a control a KeyBinding you need to add it to its InputBindings.

For example if you want to execute a command when you press F2 or pressing a button, you can add a key binding to the window’s InputBindings as follows:

      In XAML:

     <Window x:Class="WpfApplication1.Window1"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            Title="Window1" Height="300" Width="300">

        <Window.InputBindings>

            <KeyBinding Command="{Binding RefreshFilesListCommand}" Key="F2"></KeyBinding>

        </Window.InputBindings>

        <Grid>

            <Button Command="{Binding RefreshFilesListCommand}">Test Binging</Button>

        </Grid>

      </Window>

 

Or the same can be accomplished in code:        

       public Window1()

       {

                  InitializeComponent();           

                  this.InputBindings.Add(new KeyBinding(OrdersCommands.MySimpleCommand, new KeyGesture(Key.F2)));

       }

 

If you want to put the binding in a userControl you can use the UserControl's Focus method to set the focus on that control. You might find useful this MSDN article: Focus Overview

 

Hope it helps!

 

Matias Bonaventura

http://blogs.southworks.net/matiasb

 

 

 

Apr 11, 2009 at 2:42 PM

Hi findjammer!

I have encountered the same issue with keybindings. Unfortunately, the two solutions from Matias don’t work. The XAML solution won’t compile because you cannot define a binding within the KeyBinding Command property (it’s not a DependencyProperty).  And the code solution works only with commands exposed through static properties.
 
You might find my approach useful. See http://compositeextensions.codeplex.com/Thread/View.aspx?ThreadId=52980.

jbe