0

I have a DataGrid which displays a RowDetailTemplate for the currently selected row. The user can scroll through the DataGrid using the up and down arrow keys easily, but when the user reaches the last row in the DataGrid something odd happens: Instead of keeping the focus on the last row it enters the RowDetailTemplate. I wouldn't mind loosing the focus in the row selection, but the problem is that the first UserControl in the RowDetailTemplate is a ComboBox which then also reacts to the down arrow key by changing its selection. I have some few customers that already that complained that they changed the values in that ComboBox unintentionally, because they did not notice that the focus moved from the row into the ComboBox.

The only solution that I found so far is to set the IsTabStop to false, but this also means that the user cannot use the tab key anymore to reach the said ComboBox.

Does anybody have an idea how to prevent the focus to enter the RowDetailTemplate via the arrow key?

Sheridan
  • 68,826
  • 24
  • 143
  • 183
Ralf
  • 293
  • 5
  • 15

1 Answers1

0

It would be nice if you guys read each other's questions because I just answered an almost identical question to this one earlier today. Take a look at my answer to the WPF C# How to disbale Focus changes by Arrow Keys question for more information. However, your question is just different enough to stop me from closing it as a duplicate.

The principle remains the same however... you need to set the e.Handled property to true in a PreviewKeyDown event handler to stop the normal behaviour (from the linked question):

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Left || e.Key == Key.Right)
    {
        // Move your camera here
        e.Handled = true;
    }
}

The only difference is that you'd need to check if you've got to the last item before you cancel the normal behaviour.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183