Keyboard Support
The DataForm allows you to navigate through the items without using the mouse.
The utilization of the keys can entirely replace the usage of the mouse by providing shortcuts for navigation and editing.
Shortcuts
The DataForm supports the following keyboard shortcuts:
SHORTCUT | DESCRIPTION |
---|---|
Right Arrow |
Moves the focus one cell to the right. |
Left Arrow |
Moves the focus one cell to the left. |
Ctrl & Home
|
Moves the current item to the first position. |
Ctrl & Left Arrow
|
Moves the current item to the first position. |
Ctrl & End
|
Moves the current item to the last position. |
Ctrl & Right Arrow
|
Moves the current item to the last position. |
F2 |
Starts the edit mode. |
Insert |
Adds a new item. |
Enter |
Commits the edit operation. |
Escape |
Cancels the edit operation. |
Delete |
Deletes the current item. |
Custom Keyboard Command Provider
The DataForm provides an MVVM-friendly approach for customizing of the default logic of its commands.
The component also enables you to extend the way the component handles a particular key by creating your own custom command provider and predefining the behavior for that key.
The following example demonstrates the implementation of this approach—you have to create a separate class, inherit the DataFormCommandProvider
, and then override the ProvideCommandsForKey(KeyEventArgs args)
method. In this way you will adjust only the required undesired behavior.
Create Custom KeyboardCommandProvider
public class CustomKeyboardCommandProvider : DataFormCommandProvider
{
public CustomKeyboardCommandProvider()
: base(null)
{
}
public CustomKeyboardCommandProvider(RadDataForm dataForm)
: base(dataForm)
{
this.DataForm = dataForm;
}
public override List<DelegateCommandWrapper> ProvideCommandsForKey(KeyEventArgs args)
{
List<DelegateCommandWrapper> actionsToExecute = base.ProvideCommandsForKey(args);
if (args.Key == Key.Right)
{
actionsToExecute.Clear();
actionsToExecute.Add(new DataFormDelegateCommandWrapper(RadDataFormCommands.MoveCurrentToNext, this.DataForm));
actionsToExecute.Add(new DataFormDelegateCommandWrapper(RadDataFormCommands.BeginEdit, this.DataForm));
}
if (args.Key == Key.Left)
{
actionsToExecute.Clear();
actionsToExecute.Add(new DataFormDelegateCommandWrapper(RadDataFormCommands.MoveCurrentToPrevious, this.DataForm));
actionsToExecute.Add(new DataFormDelegateCommandWrapper(RadDataFormCommands.BeginEdit, this.DataForm));
}
if (actionsToExecute.Count > 0)
{
actionsToExecute.Add(new DataFormDelegateCommandWrapper(new Action(() => { this.DataForm.AcquireFocus(); }), 100, this.DataForm));
args.Handled = true;
}
return actionsToExecute;
}
}
Left Arrow
or Right Arrow
keys, the focus will move to the next or previous item respectively and edit it. Remember to remove the predefined commands for that particular key by calling the Clear()
method.
The last thing to do is set the CommandProvider
property of the DataForm to the newly-created CustomKeyboardCommandProvider
class:
Set CommandProvider in XAML
<Grid.Resources>
<my:CustomKeyboardCommandProvider x:Key="CustomProvider"/>
</Grid.Resources>
<telerik:RadDataForm x:Name="RadDataForm1"
ItemsSource="{Binding Employees}"
CommandProvider="{StaticResource CustomProvider}"/>
Set CommandProvider in Code
this.RadDataForm1.CommandProvider = new CustomKeyboardCommandProvider(this.RadDataForm1);
Disabling the Built-in Navigation
The DataFormCommandProvider
provides a way to disable the built-in navigation by setting the EnableBuiltInNavigation
property to false
. By default, the built-in navigation is enabled and EnableBuiltInNavigation
is set to True
.
Set EnableBuiltInNavigation to False
public class NoBuiltInNavigationKeyboardCommandProvider : DataFormCommandProvider
{
public NoBuiltInNavigationKeyboardCommandProvider()
: base(null)
{
}
public NoBuiltInNavigationKeyboardCommandProvider(RadDataForm dataForm)
: base(dataForm)
{
this.DataForm = dataForm;
this.EnableBuiltInNavigation = false;
}
}
Controlling Handled Events Processing
The DataFormCommandProvider
class exposes a way to manually process the handled events by setting the ShouldProcessHandledEvents
of the DataFormCommandProvider
to false
. By default, ShouldProcessHandledEvents
is set to true
.
Set ShouldProcessHandledEvents to False
public class ProcessHandledEventsKeyboardCommandProvider : DataFormCommandProvider
{
public ProcessHandledEventsKeyboardCommandProvider()
: base(null)
{
}
public ProcessHandledEventsKeyboardCommandProvider(RadDataForm dataForm)
: base(dataForm)
{
this.DataForm = dataForm;
this.ShouldProcessHandledEvents = false;
}
}