How to change the PageUp and PageDown key behavior in WPF DataGrid?
WPF DataGrid (SfDataGrid) when you press the PageUp or PageDown key will be scrolled to the previous set of rows or the next set of rows that are not displayed in the view. To change this behavior like Tab key navigation that moves the currentcell to next cell of the same row by overriding GridCellSelectionController / GridSelectionController class and setting it to SfDataGrid.SelectionController property.
When SelectionUnit is a Cell, you have to override GridCellSelectionController and when SelectionUnit is Row, you have to override GridSelectionController class.
WPF DataGrid (SfDataGrid) is defined with SelectionUnit as Cell. You can change the PageUp and PageDown key behavior by overriding the ProcessKeyDown method in GridCellSelectionController in WPF DataGrid (SfDataGrid).
<syncfusion:SfDataGrid x:Name="sfDataGrid" SelectionMode="Single" SelectionUnit="Cell" ItemsSource="{Binding Orders}" AutoGenerateColumns="False"/>
// set the customized GridCellSelectionControllserExt to SfDataGrid.SelectionController when CellSelection applied in SfDataGrid this.sfDataGrid.SelectionController = new GridCellSelectionControllerExt(sfDataGrid); //Inherits the GridCellSelectionController Class public class GridCellSelectionControllerExt : GridCellSelectionController { public GridCellSelectionControllerExt(SfDataGrid datagrid) : base(datagrid) { } //overriding the ProcessKeyDown Event from GridCellSelectionController base class protected override void ProcessKeyDown(KeyEventArgs args) { if (args.Key == Key.PageUp || args.Key == Key.PageDown) { //Key based Customization 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 PageUp and PageDown key args.Handled = arguments.Handled; return; } base.ProcessKeyDown(args); } }
WPF DataGrid (SfDataGrid) is defined with SelectionUnit as Row. You can change the PageUp and PageDown key behavior by overriding the ProcessKeyDown method in GridSelectionController in WPF DataGrid (SfDataGrid).
<syncfusion:SfDataGrid x:Name="sfDataGrid" SelectionMode="Single" SelectionUnit="Row" ItemsSource="{Binding Orders}" AutoGenerateColumns="False">
// set the customized GridSelectionControllserExt to SfDataGrid.SelectionController when RowSelection applied in SfDataGrid this.sfDataGrid.SelectionController = new GridSelectionControllerExt(sfDataGrid); //Inherits the GridSelectionController Class public class GridSelectionControllerExt : GridSelectionController { public GridSelectionControllerExt(SfDataGrid datagrid) : base(datagrid) { } //overriding the ProcessKeyDown Event from GridSelectionController base class protected override void ProcessKeyDown(KeyEventArgs args) { if (args.Key == Key.PageUp || args.Key == Key.PageDown) { //Key based Customization 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 PageUp and PageDown key args.Handled = arguments.Handled; return; } base.ProcessKeyDown(args); } }
Take a moment to peruse the WPF DataGrid – Selection documentation, where you can find about selection with code examples.
You can download the example from GitHub.
Conclusion
I hope you enjoyed learning how to change the PageUp and PageDown key bahavior in WPF DataGrid.
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!