DataGrid Tips & Tricks: Single-Click Editing

By default, end users must double-click a cell in the DataGrid to enter edit mode. In certain cases, it may be more desirable for cells to enter edit mode with a single mouse click (for example, when editing a CheckBox). Use the code snippets below to implement
single-click editing in your DataGrid.

To apply single-click editing to all cells in the DataGrid
1. Paste the style below into the Resources of your DataGrid
2. Paste the method into the code behind

To apply single-click editing to only certain cells in the DataGrid
1. Set an x:Key on the Style (ex. <Style x:Key="SingleClickEditing" TargetType="{x:Type dg:DataGridCell}">)
2. Paste the style into the Resources of your DataGrid
3. Apply the style to the CellStyle property of the columns which you'd like to have single click editing (ex. <dg:DataGridCheckBoxColumn CellStyle="{StaticResource SingleClickEditing}"/>)
4. Paste the method into the code behind

Is there any way to add the style in code behind. Since i have create custom control for datagrid by inherting wpf toolkit datagrid. i want the style to be added in code behind. Any idea to implement the same would be appreciated.

I placed the style shown above in a new Style that I've defined in a resource dictionary but when I try to build I get the following error.

The event 'PreviewMouseLeftButtonDown' cannot be specified on a Target tag in a Style. Use an EventSetter instead.

Is this possible? I'm attempting to wrap the DataGrid up with a control that inherits from the WpfToolkit DataGrid so that I can add the functionality described here to a single control that I can reuse throughout different portions of my app.

The above code works great for me. I do have a question though. After the first click into a cell, is there a way to programmatically force input into the provided editor? For instance, if I have a combobox, I'd like for the combobox to be expanded when the user performs the first click. This would eliminate yet another click, which is important to my endusers.

I managed to find a way that suited my needs (reverse tabbing still doesn't work).

Here is one of my columns (I decided NO to textblocks - and have a single cell template): <my:DataGridTemplateColumn Header="Item #"> <my:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Path=ItemNumber}" /> </DataTemplate> </my:DataGridTemplateColumn.CellTemplate> </my:DataGridTemplateColumn>I also have similar column for a CheckBox (I didn't like the built in column).

Private Sub DataGridCell_GotFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Dim cell As DataGridCell = CType(sender, DataGridCell) Dim childTxt As TextBox = FindVisualChild(Of TextBox)(cell) If childTxt IsNot Nothing Then If Not childTxt.IsFocused Then Keyboard.Focus(childTxt) End If Exit Sub End If

Dim childChk As CheckBox = FindVisualChild(Of CheckBox)(cell) If childChk IsNot Nothing Then If Not childChk.IsFocused Then Keyboard.Focus(childChk) End If Exit Sub End If End Sub