Keyboard Command Provider

RadGridView provides a set of keyboard navigation scenarios that will result in certain consequence of commands to be executed. However, in case you require them not to be invoked at all or to utilize different commands, you may take advantage of the IKeyboardCommandProvider Interface and implement your custom logic. Thus, you will be able to change the behavior of all keys. The hurdle here is the requirement for predefining each one of the commands.

Another approach for accomplishing the purpose will be to create a separate class, inheriting the DefaultKeyboardCommandProvider and overriding the ProvideCommandsForKey (Key key) method. In this way only the undesired behavior can be adjusted according to your requirements.

The custom class responsible for the update of the commands needs to be similar to the one below:

public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider 
{ 
    private GridViewDataControl parentGrid; 
 
    public CustomKeyboardCommandProvider(GridViewDataControl grid) 
     : base(grid) 
    { 
        this.parentGrid = grid; 
    } 
 
    public override IEnumerable<ICommand> ProvideCommandsForKey(Key key) 
    { 
        List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList(); 
 
        if (key == Key.Enter) 
        { 
            commandsToExecute.Clear(); 
            commandsToExecute.Add(RadGridViewCommands.CommitEdit); 
            commandsToExecute.Add(RadGridViewCommands.MoveNext); 
            commandsToExecute.Add(RadGridViewCommands.BeginEdit); 
        } 
 
        return commandsToExecute; 
    } 
} 
Public Class CustomKeyboardCommandProvider 
    Inherits DefaultKeyboardCommandProvider 
    Private parentGrid As GridViewDataControl 
 
    Public Sub New(grid As GridViewDataControl) 
        MyBase.New(grid) 
        Me.parentGrid = grid 
    End Sub 
 
    Public Overrides Function ProvideCommandsForKey(key As Key) As IEnumerable(Of ICommand) 
        Dim commandsToExecute As List(Of ICommand) = MyBase.ProvideCommandsForKey(key).ToList() 
        If key = Key.Enter Then 
            commandsToExecute.Clear() 
            commandsToExecute.Add(RadGridViewCommands.CommitEdit) 
            commandsToExecute.Add(RadGridViewCommands.MoveNext) 
            commandsToExecute.Add(RadGridViewCommands.BeginEdit) 
        End If 
        Return commandsToExecute 
    End Function 
End Class 

Following up the code-snippet above, a press of Enter key will result in saving the current changes, moving the focus to the right cell 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 KeyboardCommandProvider Property of the RadGridView to be the newly-created CustomKeyboardCommandProvider class:

this.RadGridView1.KeyboardCommandProvider = new CustomKeyboardCommandProvider(this.RadGridView1); 
Me.RadGridView1.KeyboardCommandProvider = New CustomKeyboardCommandProvider(Me.RadGridView1) 

You can download a runnable project of the previous example from our online SDK repository here.

You can also check the SDK Samples Browser that provides a more convenient approach in exploring and executing the examples in the Telerik XAML SDK repository.

For some additional details you can also check this blog post

In this article