Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf datagrid Enter key move next row

Tags:

c#

wpf

datagrid

I'm trying to develop a wpf app with a datagrid, and i want to allow the users to enter values like in excel.

Example: The datagrid has 2 columns, Name and BarCode The user is editing the BarCode on the first row, and when the user press Enter key, the focus should move the the row below on the BarCode cell.

The user must be able to use a barcode scanner to register the barcodes, on the existing products list, without need to user the mouse or the keyboard.

Any ideas on how to implement this behavior?

Thanks, Frederico

like image 352
Frederico Regateiro Avatar asked Oct 22 '15 23:10

Frederico Regateiro


1 Answers

A simpler solution is capture the KeyDown event and if the key is 'Enter', then move to the next tab.

private void dg_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var u = e.OriginalSource as UIElement;
    if (e.Key == Key.Enter && u != null)
    {
        e.Handled = true;
        u.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
    }
}
like image 177
Ali Bayat Avatar answered Oct 25 '22 14:10

Ali Bayat