Articles in this section
Category / Section

How to change the Enter key behavior to insert line for Silverlight Grid?

2 mins read

In SfDataGrid, by default when you press Enter key, the CurrentCell moves to next row. To change this behavior like inserting a line break by pressing the Enter key when the CurrentCell is in the edit mode, you can change this Enter key behavior by overriding GridCellSelectionController / GridSelectionController class and setting it to SfDataGrid.SelectionController property.

When SelectionUnit is Cell, you have to override GridCellSelectionController and when SelectionUnit is Row, you have override GridSelectionController class.

You can insert the line break within the TextBox by setting AcceptsReturn to true. AcceptsReturn property allows you to use the TextBox as a multiline input field.

In the following code example, SfDataGrid is defined with SelectionUnit as Cell and Style for TextBox within SfDataGrid.Resources.ResourceDictionary is defined by setting AcceptsReturn to true.

XAML

<syncfusion:SfDataGrid x:Name="sfdatagrid" 
                       RowHeight="50"
                       AllowEditing="True"
                       NavigationMode="Cell"
                       SelectionUnit="Cell"
                       SelectionMode="Single"
                       AllowResizingColumns="False"
                       ItemsSource="{Binding Products}">
 
    <syncfusion:SfDataGrid.Resources>
        <ResourceDictionary>
            <Style TargetType="TextBox">
                <Setter Property="AcceptsReturn" Value="True"/>
            </Style>
        </ResourceDictionary>
    </syncfusion:SfDataGrid.Resources>
</syncfusion:SfDataGrid>

The following code example shows you how to set the instance of GridCellSelectionControllerExt that is derived from GridCellSelectionController to SfDataGird.SelectionController property.

C#

public MainWindow()
{
InitializeComponent();
this.sfdatagrid.SelectionController = new GridCellSelectionControllerExt(sfdatagrid);
}

The following code example explains you how to override the GridCellSelectionController class.

C#

using Syncfusion.UI.Xaml.Grid;
public class GridCellSelectionControllerExt : GridCellSelectionController
{
public GridCellSelectionControllerExt(SfDataGrid dataGrid)
    : base(dataGrid)
    {
           
    }
}

By default SfDataGrid handles Enter key. You can change this behavior by setting KeyEventArgs.Handled to false in ProcessKeyDown override method as in the following code example.

C#

using System.Windows.Input;
public class GridCellSelectionControllerExt : GridCellSelectionController
{
    public GridCellSelectionControllerExt(SfDataGrid dataGrid)
    : base(dataGrid)
    {
    }
    protected override void ProcessKeyDown(KeyEventArgs args)
    {
        if(args.Key == Key.Enter)
        {
            args.Handled = false;
            return;
        }
        base.ProcessKeyDown(args);
    }
}

 

Note:

For WinRT platform, no need to override GridCellSelectionController / GridSelectionController. Setting AcceptsReturn property as True to TextBox within SfDataGrid.Resources.ResourceDictionary enables multiline input.

 

In the following screenshot SfDataGrid allows you to enter multiline inputs while editing.

D:\Issuesample\KB's\EnterKey\WithText.png

 

 

Sample Links:

 

WPF

WRT
SilverLight

UWP

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied