DataGrid: Entering EditMode upon typing

Mar 12, 2010 at 10:46 PM
Edited Mar 12, 2010 at 10:47 PM

I have a DataGridTemplateColumn.  Its default template (non-edit mode) is simply a TextBlock.  Its CellEditingTemplate contains a custom UserControl.

I have a couple of questions:

1) I want the cell to enter edit mode when the user starts typing, just like the DataGridTextColumn does.  How do I do this?

2) I want a certain child control on my UserControl to receive focus when the cell enters edit mode.  How do I do this?



Mar 19, 2010 at 6:20 PM

1) In the datagrid KeyDown or PreviewKeyDown event (if you want to pass the first character entered by the user) use datagridName.BeginEdit().

If you use KeyDown you will not be able to catch keys like Delete, Enter, or Tab.  They will be processed by the datagrid.  If you need to pass them to your control, use the PreviewKeyDown event.

2) In the PreparingCellForEdit event use the Focus method.  Example (moves the focus on the combobox - part of the datagridtemplatecolumn):

If TypeOf (VisualTreeHelper.GetChild(e.EditingElement, 0)) Is ComboBox Then
                CType(VisualTreeHelper.GetChild(e.EditingElement, 0), ComboBox).Focus()
End If