I'm having trouble with @surfen's answer, probably because it is many years later, and WPF probably changed a bit. It seems that DataGrid now takes care of some things for you, for example, it automatically edits a text field when you start typing.
I use a DataGridTemplateColumn for a combo column. The template has a TextBlock for its CellTemplate . A BeginEdit call, followed by a dispatcher call, invokes a combo box in the visual tree. Then a message appears that the mouse click is sent to the combo box and opens by itself.
Here is my modified version of @surfen code:
public static class DataGridExtensions { public static void FastEdit(this DataGrid dataGrid) { dataGrid.ThrowIfNull(nameof(dataGrid)); dataGrid.PreviewMouseLeftButtonDown += (sender, args) => { FastEdit(args.OriginalSource, args); }; } private static void FastEdit(object source, RoutedEventArgs args) { var dataGridCell = (source as UIElement)?.FindVisualParent<DataGridCell>(); if (dataGridCell == null || dataGridCell.IsEditing || dataGridCell.IsReadOnly) { return; } var dataGrid = dataGridCell.FindVisualParent<DataGrid>(); if (dataGrid == null) { return; } if (!dataGridCell.IsFocused) { dataGridCell.Focus(); } if (dataGridCell.Content is CheckBox) { if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!dataGridCell.IsSelected) { dataGridCell.IsSelected = true; } } else { var dataGridRow = dataGridCell.FindVisualParent<DataGridRow>(); if (dataGridRow != null && !dataGridRow.IsSelected) { dataGridRow.IsSelected = true; } } } else { dataGrid.BeginEdit(args); dataGridCell.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { })); } } } public static class UIElementExtensions { public static T FindVisualParent<T>(this UIElement element) where T : UIElement { UIElement currentElement = element; while (currentElement != null) { var correctlyTyped = currentElement as T; if (correctlyTyped != null) { return correctlyTyped; } currentElement = VisualTreeHelper.GetParent(currentElement) as UIElement; } return null; } }
NathanAldenSr
source share