How pass along a KeyBinding of a UserControl to its parent?

I've created a little OKCancelControl and I'd like to bind a command when user press Enter or Escape

Here's the XAML

    <KeyBinding Key="Enter"
                Command="{Binding ElementName=this, Path=OkCommand, UpdateSourceTrigger=PropertyChanged}"/>

    <KeyBinding Key="Escape"

        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    <Border Grid.Column="0"
            Style="{StaticResource BorderStyle}">
        <Button Content="{Binding ElementName=this, Path=ButtonName, UpdateSourceTrigger=PropertyChanged}"
                Command="{Binding ElementName=this, Path=OkCommand, UpdateSourceTrigger=PropertyChanged}"/>

    <Border Grid.Column="1"
            Style="{StaticResource BorderStyle}">
        <Button Content="{Binding Source={x:Static Member=t:BaseText.Cancel}}"
                Command="iw:InnerWindow.CancelCommand" />

But the KeyBinding will be handled only if my UserControlhas the focus which is not good in my case.

I'd like to find a MVVM or XAML way (if possible) to let the parent "listen" to user key press.

Thank you in advance ;)


OK and Cancel buttons using Enter and Escape is something that Windows has standardised on for a long time, and so there's a built-in solution for this. You don't need separate keybindings for the commands, because you can set the buttons up to respect these keys.

To make WPF a button be "pressed" when you hit enter, set IsDefault="True" in the button's Xaml. Similarly, setting IsCancel="True" will cause the button to be pressed when you hit Escape.

Both of these keys should then work from anywhere in the active Window, regardless of the focus of your control. It's worth noting that the behaviour can be unpredictable if you have more than one Default or Cancel button in your Window.

Need Your Help

Difference between exploration and exploitation in genetic algorithm

search genetic-algorithm genetic-programming

In evolutionary algorithms two main abilities maintained which are Exploration and Exploitation.

could gSoap be integrated with the Google app engine?

google-app-engine gsoap

We are using GAE to host our web services, as far as I know GAE only support Java and python at the moment, however most of our engineers here are more comfortable with C/C++, so i was wondering if...