How to add new row to TAB key in WPF dataGrid

I want to add a new row to my datagrid when I press the "TAB" key in the last datagrid cell.

I am using the MVVM pattern for this. I came up with a solution, I comprehended the Tab key on datagrid input binding:

    <DataGrid.InputBindings>
       <KeyBinding Command="{Binding Path=InsertNewLineCommand}" Key="Tab"></KeyBinding>
   </DataGrid.InputBindings>

And added the following code to InsertNewLineCommand:

private void ExecuteInsertNewLineCommand()
    {
        //Checked is SelectedCell[0] at last cell of the datagrid
        {
            InsertNewLine();
        }
    }

But the problem is that ON ADDING KEYBINDING = 'TAB' MY NORMAL FEATURE OF THE TABLE ON A NETWORK DISCONNECTION (MOVING TO THE NEXT CELL AND SO ...)

+3
source share
1 answer

Just determine if you are in the last column, and then run your command.

PreviewKeyDown, , executeCommand. , :

<DataGrid PreviewKeyDown="DataGrid_PreviewKeyDown" SelectionUnit="Cell" ....

private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (!Keyboard.IsKeyDown(Key.Tab)) return;
    var dataGrid = (DataGrid) sender;

    var current = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn);
    var last = dataGrid.Columns.Count - 1;

    if (current == last)
         ExecuteInsertNewLineCommand();

}

+1

All Articles