KeyToCommand

The KeyToCommand class is a special behavior that allows you to bind a KeyGesture to a command.

Getting Started with KeyToCommand

Assume that there is a requirement to handle a the key gesture of a control. For example, you need to invoke the ViewModel's CommitCommand when the end-user presses the Enter key while the focus is in the TextBox.

[POCOViewModel]
public class MainViewModel {
    ...
    public void Commit() {
        ...
    }
}

To accomplish this task, you can use the KeyToCommand behavior. Attach the KeyToCommand behavior to the TextBox's dxmvvm:Interaction.Behaviors collection.

<TextBox>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:KeyToCommand />
    </dxmvvm:Interaction.Behaviors>
</TextBox>

Specify the key gesture that should be handled in the KeyToCommand.KeyGesture property and bind the EventToCommandBase.Command property to the CommitCommand.

<TextBox>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:KeyToCommand KeyGesture="Enter" Command="{Binding CommitCommand}"/>
    </dxmvvm:Interaction.Behaviors>
</TextBox>

Note that KeyToCommand uses the KeyUp event to handle KeyGestures by default. If necessary, you can specify any other event here, for example, KeyDown.

Due to the fact that the KeyToCommand and EventToCommand are inherited from one base class, their overall capabilities are similar to ProcessEventsFromDisabledEventOwner, MarkRoutedEventsAsHandled, UseDispatcher and DispatcherPriority.