Question

I am using WPFToolKit DataGrid to display data; In my use case

  1. user clicks on a cell and enters a value (I trap the event and set the focus to the TextBox)
  2. Presses Enter
  3. Value is committed
  4. Focus shifts to Next Cell

Now, user can't enter any value in the TextBox(in the DataGridCell) unless he clicks on the cell. The TextBox can be a part of various controls(like NumericUpDown, Calendar etc.).

This behavior is similar to Excel but I am not able to shift the focus to the Underlying TextBox as various other wrapper user controls are there in the DataGridCell(something like DataGridCell contains MatrixCellContainer, which contains MatrixCell, which contains UpDown Control)

Any pointer will be really helpful.

Update:

I can achieve what I am looking for by handling DataGridCell_Selected event like this :

private void DataGridCell_Selected(object sender, RoutedEventArgs e)
{
        Microsoft.Windows.Controls.DataGridCell dataGridCell = 
               sender as Microsoft.Windows.Controls.DataGridCell;

    // ToDo: This is a very bad hack; 
    // should be replaced by some proper technique
    if (dataGridCell != null)
    {
        NumericUpDownBase<int>[] IntUpDownControls = 
            dataGridCell.GetChildrenOfType<NumericUpDownBase<int>>();
        if (IntUpDownControls.Count() != 0)
        {
            IntUpDownControls[0].Focus();
            //Keyboard.Focus(IntUpDownControls[0]);
        }
    else
    {
        NumericUpDownBase<double>[] DblUpDownControls = 
                dataGridCell.GetChildrenOfType<NumericUpDownBase<Double>>();
         if (DblUpDownControls.Count() != 0)
         {
                 DblUpDownControls[0].Focus();
                 //Keyboard.Focus(DblUpDownControls[0]);
          }
    }
    }
 }

But I know there will be a better way to achieve this!

Was it helpful?

Solution 2

Finally I settled with this -

private void HandleCellSelected(object sender, RoutedEventArgs e)
{
    DataGridCell dataGridCell = sender as DataGridCell;
    if (dataGridCell != null)
    {
        TextBox[] textboxcontrols = dataGridCell.GetChildrenOfType<TextBox>();
        if (textboxcontrols.Count() != 0)
        {
            textboxcontrols[0].Focus();
        }
    }
}

Still looking for a better approach though...

OTHER TIPS

How are you setting Focus to the next cell?

WPF has two versions of focus, Logical Focus and Keyboard Focus. I suspect you are using myDataGridCell.Focus(), which only sets Logical Focus.

myDataGridCell.Focus();         // Sets Logical Focus
Keyboard.Focus(myDataGridCell); // Sets Keyboard Focus
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top