KeyToCommand

The DevExpress.WinUI.Core.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 key gestures of a control. For example, you need to invoke the ViewModel’s CommitCommand when the user presses the Enter key while the focus is in the TextBox.

public class MainViewModel {
    ...
    public void Commit() {
        ...
    }
}

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

<UserControl
    xmlns:dxmvvmi="using:DevExpress.WinUI.Mvvm.UI.Interactivity"
    xmlns:dxmvvm="using:DevExpress.WinUI.Mvvm.UI"
    ...>
    <TextBox>
        <dxmvvmi:Interaction.Behaviors>
            <dxmvvm:KeyToCommand />
        </dxmvvmi:Interaction.Behaviors>
    </TextBox>
</UserControl>

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

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

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

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