4
votes

I have a datagrid in a Silverlight application. Clicking on the first row, and then pressing the Enter key causes the selection to move to the next row. I do not want this behavior - the Enter key is used for totally different purposes on this screen.

I realize that this is part of the editing framework, but I need a way to turn it off. I tried setting IsReadOnly to True (even though the control isn't technically read-only) and that didn't have any effect.

I attached to the datagrid KeyDown event but it's not called when the Enter key is pressed. It works fine for other keys.

I'm stumped. Thanks for your help.

1

1 Answers

4
votes

Yeah, that's annoying.

As far as I know, the only option is to create your own control that inherits from DataGrid, and do what is needed before passing the event on to the base.

public class NewDataGrid : DataGrid 
{     
    protected override void OnKeyDown(KeyEventArgs e)     
    {         
        if (e.Key == Key.Enter)         
        {
            //do what is needed here             
            e.Handled = true;         
        }         
        base.OnKeyDown(e);     
    } 
}