Skip to main content Skip to footer

How to fix focus issue when custom C1ComboBox editor receives focus in FlexGrid cell

When using a ComboBox as an editor in a DataGrid or FlexGrid, the control's default behavior is to select all text when it receives focus. This creates an issue for "type-ahead" scenarios: if a user types 'A' to start editing, the grid opens the ComboBox, sets the text to 'A', and then immediately selects it. When the user types 'B', the selected 'A' is overwritten, making it impossible to type multiple characters naturally.

Solution
To prevent this overwriting, you need to manually move the text cursor (caret) to the end of the text string when the ComboBox gains focus, rather than allowing it to select the entire content.

Since the ComboBox in many frameworks is a composite control, you must access its internal template to find the actual text entry element. By handling the grid's BeganEdit event, you can hook into the GotFocus event of the specific ComboBox instance being used as the editor. Within that focus event, find the internal EditTextBox and use the Select method to set the selection length to zero and the start index to the end of the string. This ensures the caret is placed after the first character typed, allowing subsequent keystrokes to append rather than overwrite.

void grid_BeganEdit(object sender, DataGridBeganEditEventArgs e)
{
     if (e.Column.Index == 0 || e.Column.Index == 1)
     {
          var combo = e.EditingElement as C1ComboBox;
          combo.GotFocus += ComboBox_GotFocus;
          combo.IsEditable = true;
     }
}

private void ComboBox_GotFocus(object sender, RoutedEventArgs e)
{
    var temp = (sender as C1ComboBox).Template.FindName("ComboHeader", (sender as C1ComboBox)) as C1TextEditableContentControl;
    temp.EditTextBox.Select(temp.EditTextBox.Text.Length,0);
}