How to change the Enter key behavior in SfDataGrid?
By default, in the SfDataGrid on pressing the Enter key, the currentcell is moved to the next row when NavigationMode is Cell. You can change this behavior like Tab key navigation that moves the currentcell to next cell of the same row by overriding the GridSelectionController.
The following code example shows you how to override the GridSelectionController.
C#
//Inherits the GridSelectionController Class public class GridSelectionControllerExt : GridSelectionController { public GridSelectionControllerExt(SfDataGrid datagrid) : base(datagrid) { } }
To customize the Enter key behavior, you need to override the ProcessKeyDown method in GridSelectionController. In this method, you can change the Enter key behavior like Tab key by changing the Enter KeyEventArgs to Tab KeyEventArgs and also you can assign the state of Tab key event handling args to Enter key event handling args.
The following code example illustrates how to change the Enter KeyEventArgs into Tab KeyEventArgs in ProcessKeyDown method.
C#
//overriding the ProcessKeyDown Event from GridSelectionController base class protected override void ProcessKeyDown(KeyEventArgs args) { if (args.Key == Key.Enter) { KeyEventArgs arguments = new KeyEventArgs(args.KeyboardDevice, args.InputSource, args.Timestamp, Key.Tab) { RoutedEvent = args.RoutedEvent }; base.ProcessKeyDown(arguments); //assigning the state of Tab key Event handling to Enter key args.Handled = arguments.Handled; return; } base.ProcessKeyDown(args); }
The following code example shows you how to set the instance of above GridSelectionControllerExt that is derived from GridSelectionController to SfDataGrid.SelectionController property.
C#
public MainWindow() { InitializeComponent(); // set the customized GridSelectionControllserExt to SfDataGrid.SelectionController this.dataGrid.SelectionController = new GridSelectionControllerExt(dataGrid); }
You can refer to the following sample to change the Enter key behavior.
Sample Link: CustomizeEnterKeyBehavior_WPF