wpf-devexpress-dot-mvvm-dot-ui-1f952dd2.md
Allows you to bind a KeyGesture to a command.
Namespace : DevExpress.Mvvm.UI
Assembly : DevExpress.Xpf.Core.v25.2.dll
NuGet Package : DevExpress.Wpf.Core
public class KeyToCommand :
EventToCommandBase
Public Class KeyToCommand
Inherits EventToCommandBase
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() {
// ...
}
}
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Public Class MainViewModel
Inherits ViewModelBase
<Command>
Public Sub Commit()
End Sub
End Class
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() {
// ...
}
}
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Public Class MainViewModel
Inherits ViewModelBase
<Command>
Public Sub Commit()
End Sub
End Class
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() {
// ...
}
}
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Public Class MainViewModel
Inherits ViewModelBase
<Command>
Public Sub NewMessage()
End Sub
End Class
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.
wpf-scheduler-hide-non-working-days-in-day-view/CS/NavigateCommandExample/MainWindow.xaml#L33
<dxmvvm:Interaction.Behaviors>
<dxmvvm:KeyToCommand Command="{DXCommand Execute='MyMoveLeft(@e(scheduler))'}" KeyGesture="Left" />
<dxmvvm:KeyToCommand Command="{DXCommand Execute='MyMoveRight(@e(scheduler))'}" KeyGesture="Right" />
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