How to enable one-click editing on the table?
Normally, it requires 2 clicks to enter into edit mode in a grid-cell. This set of code will let you do that in 1 click.
In XAML:
|
<!-- To enable single click editing of grid cells. --> <Style TargetType="{x:Type DataGridCell}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter> </Style> |
In C#:
|
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridCell cell = sender as DataGridCell; if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } DataGrid dataGrid = Extensions.FindAncestor<DataGrid>(cell); if (dataGrid != null) { if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) cell.IsSelected = true; } else { DataGridRow row = Extensions.FindAncestor<DataGridRow>(cell); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } } } |
� RadiantQ 2009 - 2019. All Rights Reserved.