Overriding Enter Key Behavior Problem

Hi,

I'm trying to handle the Enter KeyDown event myself instead of having the cell in the next row get selected. I'd also like to remove the Tab key function too.

Following this does that but causes more problems:

https://www.syncfusion.com/kb/3815/how-to-change-the-enter-key-behavior-in-sfdatagrid

Is it seems to override my cell selection mode and forces it to full row selection instead of individual cell selection. I can't figure out how to change it back. It's ignoring my XAML and code behind.


It also causes System.NullReferenceException: 'Object reference not set to an instance of an object.' if I double click on any cell.


Removing the selection controller override fixes those issues but I can't use the enter key obviously.


my XAML:

<syncfusion:SfDataGrid x:Name="dGrid" Grid.Column="1" Grid.Row="2"
                               BorderBrush="Transparent"
                               Background="Transparent"
                               AutoGenerateColumns="True"
                               AllowDeleting="False"
                               AllowDraggingColumns="False"
                               AllowEditing="False"
                               AllowResizingColumns="False"
                               AllowSorting="False"
                               IsReadOnly="True"
                               SelectionMode="Extended"
                               SelectionUnit="Cell"
                               ColumnSizer="SizeToCells"
                               ShowRowHeader="{Binding ShowAxisYHeader}"
                               HeaderRowHeight="{Binding AxisXHeaderHeight}"
                               RowHeaderWidth="{Binding RowHeaderWidth}"
                               RowHeight="{Binding RowHeight}"
                               AllowRowHoverHighlighting="False"
                               ItemsSource="{Binding TableDataView}"
                               ItemsSourceChanged="dGrid_ItemsSourceChanged"
                               KeyDown="dGrid_KeyDown"
                               KeyboardNavigation.AcceptsReturn="False"
                               >
        </syncfusion:SfDataGrid>



3 Replies

VS Vijayarasan Sivanandham Syncfusion Team October 26, 2021 02:54 PM UTC

Hi Declan Walsh,

Thank you for contacting Syncfusion Support.

Based on provided information the reported problem occurs due to mismatched SelectionController base type override in SfDataGrid.

You can customize the default selection behaviors by override the selection controller based on SelectionUnit property in SfDataGrid. Below are the built-in selection controllers, 
You can resolve the reported problem by override the ProcessKeyDown method in GridCellSelectionController in SfDataGrid. Please refer the below code snippet,

 
this.dGrid.SelectionController = new GridSelectionControllerExt(dGrid); 
 
public class GridSelectionControllerExt : GridCellSelectionController 
{ 
        public GridSelectionControllerExt(SfDataGrid datagrid) 
          : base(datagrid) 
        { 
        } 
 
        //overriding the ProcessKeyDown Event from GridSelectionController base class 
        protected override void ProcessKeyDown(KeyEventArgs args) 
        { 
            //here customize based on your scenario 
            if (args.Key == Key.Enter)                         
                return;             
            base.ProcessKeyDown(args); 
        } 
} 
 

For more information related to Customizing Selection Behaviors, please refer the below user guide documentation

UG Link: https://help.syncfusion.com/wpf/datagrid/selection#customizing-selection-behaviors
 

Please let us know if you have any concerns in this.
 
 
Regards, 
Vijayarasan S 



DW Declan Walsh October 27, 2021 03:43 PM UTC

Wonderful. That fixed it. Thankyou!



VS Vijayarasan Sivanandham Syncfusion Team October 28, 2021 05:18 AM UTC

Hi Declan Walsh,

Thanks for the update.

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S

Loader.
Up arrow icon