Skip to main content

DevExpress v24.1 Update — Your Feedback Matters

Our What's New in v24.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

KeyToCommand Class

Allows you to bind a KeyGesture to a command.

Namespace: DevExpress.Mvvm.UI

Assembly: DevExpress.Xpf.Core.v24.1.dll

NuGet Package: DevExpress.Wpf.Core

#Declaration

public class KeyToCommand :
    EventToCommandBase

#Remarks

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:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:DXApplication1.ViewModels">
    <UserControl.DataContext>
        <ViewModels:MainViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBox>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:KeyToCommand KeyGesture="Enter" Command="{Binding CommitCommand}"/>
            </dxmvvm: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:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:DXApplication1.ViewModels">
    <UserControl.DataContext>
        <ViewModels:MainViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBox>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:KeyToCommand KeyGesture="Enter" Command="{Binding CommitCommand}" EventName="KeyDown"/>
            </dxmvvm:Interaction.Behaviors>
        </TextBox>
    </Grid>
</UserControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;

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

#Handle Events in a Control’s Parent Element

The following code sample tunnels the KeyUp event to the MainView (as the PreviewKeyUp event) and invokes the NewMessage command if a user focuses the MainView and pressses the Enter key:

<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:DXApplication1.ViewModels">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:KeyToCommand KeyGesture="Enter" Command="{Binding NewMessageCommand}" EventName="PreviewKeyUp"/>
    </dxmvvm:Interaction.Behaviors>
    <UserControl.DataContext>
        <ViewModels:MainViewModel/>
    </UserControl.DataContext>
    <StackPanel>
        <TextBox Text="Type in a name">
        <TextBox Text="Type in a last name"/>
    </StackPanel>
</UserControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;

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

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the KeyToCommand class.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

#Inheritance

Show 13 items
Object
DispatcherObject
DependencyObject
Freezable
Animatable
DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase
DevExpress.Mvvm.UI.Interactivity.Behavior
DevExpress.Mvvm.UI.Interactivity.TriggerBase
DevExpress.Mvvm.UI.Interactivity.TriggerBase<DependencyObject>
EventTriggerBase<DependencyObject>
DevExpress.Mvvm.UI.Interactivity.EventTrigger
EventToCommandBase
KeyToCommand
See Also