Skip to main content
A newer version of this page is available. .

KeyToCommand Class

Allows you to specify a key combination that invokes a command.

Namespace: DevExpress.WinUI.Core

Assembly: DevExpress.WinUI.Core.v22.1.dll

NuGet Package: DevExpress.WinUI

Declaration

public class KeyToCommand :
    EventToCommandBase

Remarks

Run Demo: Behaviors Module in the WinUI MVVM Demo

In the following code sample, the KeyToCommand behavior invokes the CommitCommand when a user focuses a TextBox control and presses the Enter key:

<UserControl ...
    xmlns:dx="using:DevExpress.WinUI.Core"
    xmlns:ViewModels="clr-namespace:DXApplication1.ViewModels">
    <UserControl.DataContext>
        <ViewModels:MainViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBox>
            <dx:Interaction.Behaviors>
                <dx:KeyToCommand KeyGesture="Enter" Command="{Binding CommitCommand}"/>
            </dx:Interaction.Behaviors>
        </TextBox>
    </Grid>
</UserControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;

public class MainViewModel : ViewModelBase {
    [Command]
    public void Commit() {
        // ...
    }
}

Subscribe to Another Event

The KeyToCommand behavior is subscribed to the KeyUp event (the default event). The following code sample changes the subscribed event to KeyDown:

<UserControl ...
    xmlns:dx="using:DevExpress.WinUI.Core"
    xmlns:ViewModels="clr-namespace:DXApplication1.ViewModels">
    <UserControl.DataContext>
        <ViewModels:MainViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBox>
            <dx:Interaction.Behaviors>
                <dx:KeyToCommand KeyGesture="Enter" Command="{Binding CommitCommand}" EventName="KeyDown"/>
            </dx:Interaction.Behaviors>
        </TextBox>
    </Grid>
</UserControl>
See Also