Keyboard Support
RadDataForm allows you to navigate through the items without using the mouse. The keyboard can entirely replace the mouse by allowing you to perform navigation and editing. This article will walk you through the following sections.
- Keys List
- Custom Keyboard Command Provider
- Disable the Built-in Navigation
- Control the Processing of Handled Events
Keys List
Below is the list of the keys that are supported:
Left and Right arrow keys: Navigate through the items.
Ctrl + Home / Ctrl + Left arrow: Move the current item to the first position.
Ctrl + End / Ctrl + Right arrow: Move the current item to the last position.
F2: Start edit mode.
Insert: Add new item.
Enter: Commit the edit operation.
Escape: Cancel the edit operation.
Delete: Delete the current item.
Custom Keyboard Command Provider
RadDataForm provides an easy MVVM-friendly approach for customization of its default commands' logic. You can find more information in the Customizing Commands help article. However, if you want to extend the way RadDataForm handles a particular key, you could create your own custom command provider and predefine the behaviour for that key.
The approach for accomplishing this is to create a separate class, inherit the DataFormCommandProvider and override the ProvideCommandsForKey(KeyEventArgs args) method. In this way, only the undesired behavior can be adjusted according to your requirements.
The class responsible for customizing the keyboard navigation should to be similar to Example 1:
Example 1: Creating a 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;
}
}
Public Class CustomKeyboardCommandProvider
Inherits DataFormCommandProvider
Public Sub New()
MyBase.New(Nothing)
End Sub
Public Sub New(dataForm As RadDataForm)
MyBase.New(dataForm)
Me.DataForm = dataForm
End Sub
Public Overrides Function ProvideCommandsForKey(ByVal args As KeyEventArgs) As List(Of DelegateCommandWrapper)
Dim actionsToExecute As List(Of DelegateCommandWrapper) = MyBase.ProvideCommandsForKey(args)
If args.Key = Key.Right Then
actionsToExecute.Clear()
actionsToExecute.Add(New DataFormDelegateCommandWrapper(RadDataFormCommands.MoveCurrentToNext, Me.DataForm))
actionsToExecute.Add(New DataFormDelegateCommandWrapper(RadDataFormCommands.BeginEdit, Me.DataForm))
End If
If args.Key = Key.Left Then
actionsToExecute.Clear()
actionsToExecute.Add(New DataFormDelegateCommandWrapper(RadDataFormCommands.MoveCurrentToPrevious, Me.DataForm))
actionsToExecute.Add(New DataFormDelegateCommandWrapper(RadDataFormCommands.BeginEdit, Me.DataForm))
End If
If actionsToExecute.Count > 0 Then
actionsToExecute.Add(New DataFormDelegateCommandWrapper(New Action(Sub()
Me.DataForm.AcquireFocus()
End Sub), 100, Me.DataForm))
args.Handled = True
End If
Return actionsToExecute
End Function
End Class
Following up the code-snippet above, a press of Left/ Right keys will result in moving to the Next/ Previous item and editing it. However, do not forget to remove the predefined commands for that particular key by calling the Clear() method.
The last thing to be done is to set the CommandProvider property of the RadDataForm to be the newly-created CustomKeyboardCommandProvider class:
Example 2: Set the CommandProvider property
<Grid.Resources>
<my:CustomKeyboardCommandProvider x:Key="CustomProvider"/>
</Grid.Resources>
<telerik:RadDataForm x:Name="RadDataForm1"
ItemsSource="{Binding Employees}"
CommandProvider="{StaticResource CustomProvider}"/>
Example 2: Set the CommandProvider property
this.RadDataForm1.CommandProvider = new CustomKeyboardCommandProvider(this.RadDataForm1);
Me.RadDataForm1.CommandProvider = New CustomKeyboardCommandProvider(Me.RadDataForm1)
Disable the Built-in Navigation
As of Q1 2015, DataFormCommandProvider exposed the EnableBuiltInNavigation property. Its default value is True. In order to disable the built-in navigation, you can set its value to False.
Example 3: Setting the EnableBuiltInNavigation property to False
public class NoBuiltInNavigationKeyboardCommandProvider : DataFormCommandProvider
{
public NoBuiltInNavigationKeyboardCommandProvider()
: base(null)
{
}
public NoBuiltInNavigationKeyboardCommandProvider(RadDataForm dataForm)
: base(dataForm)
{
this.DataForm = dataForm;
this.EnableBuiltInNavigation = false;
}
}
Public Class NoBuiltInNavigationKeyboardCommandProvider
Inherits DataFormCommandProvider
Public Sub New()
MyBase.New(Nothing)
End Sub
Public Sub New(dataForm As RadDataForm)
MyBase.New(dataForm)
Me.DataForm = dataForm
Me.EnableBuiltInNavigation = False
End Sub
End Class
Control the Processing of Handled Events
As of Q1 2015, the new boolean property ShouldProcessHandledEvents of DataFormCommandProvider is exposed. Its default value is True. If you want to manually process handled events, you can set its value to False.
Example 4: Setting the ShouldProcessHandledEvents to False
public class ProcessHandledEventsKeyboardCommandProvider : DataFormCommandProvider
{
public ProcessHandledEventsKeyboardCommandProvider()
: base(null)
{
}
public ProcessHandledEventsKeyboardCommandProvider(RadDataForm dataForm)
: base(dataForm)
{
this.DataForm = dataForm;
this.ShouldProcessHandledEvents = false;
}
}
Public Class ProcessHandledEventsKeyboardCommandProvider
Inherits DataFormCommandProvider
Public Sub New()
MyBase.New(Nothing)
End Sub
Public Sub New(dataForm As RadDataForm)
MyBase.New(dataForm)
Me.DataForm = dataForm
Me.ShouldProcessHandledEvents = False
End Sub
End Class