I have implemented WPF DataGrid Single-Click Editing from Codeplex. In that solution, the clicked cell is focused and the row is selected to achieve single-click editing of DataGrid. It worked great.
Here's the code:
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 = FindVisualParent<DataGrid>(cell);
if (dataGrid != null)
{
if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
{
if (!cell.IsSelected)
cell.IsSelected = true;
}
else
{
DataGridRow row = FindVisualParent<DataGridRow>(cell);
if (row != null && !row.IsSelected)
{
row.IsSelected = true;
}
}
}
}
}
But I also want my DataGrid to automatically exit editing mode (without hitting Enter key) when a cell value is changed. For example, I have a combobox in the cell when in edit mode. When user selects a value in combobox, it will automatically databind the selected value. But then the user still need to click Enter to exit edit mode. How can I exit edit mode automatically?
I've tried listening for property changes and call CommitEdit function of DataGrid to exit edit mode automatically. Works great and here's the code:
void _gameCompareViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "End Edit")
{
AlignGrid.CommitEdit();
}
}
But now the Single-Click editing feature will not work for the current cell. I have to click a different row first in order for it to work. I think what I want is when CommmitEdit is called, it automatically selects a different row. (Like when you hit Enter, it will go to the next row) Any suggestions guys? Please show me codes on how to do this. Im running out of time here for my project.
Thanks for the help.
You can easily use IsReadOnly property on the DataGrid to make it entirely read only. or use IsReadOnly per Column basis to make them read only. The WPF DataGrid has an IsReadOnly property that you can set to True to ensure that users cannot edit your DataGrid 's cells.
By default, you can edit items directly in the DataGrid. The user can enter edit mode in a cell by pressing F2 key or double tapping on a cell. Alternatively, you can set the DataGridColumn. IsReadOnly property to true to disable editing in specific columns of the DataGrid.
A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns. The hierarchical inheritance of DataGrid class is as follows −
to flip from cell editing template back to cell template:
dataGrid.CancelEdit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With