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.
我已经从Codeplex实现了WPF DataGrid单击编辑。在该解决方案中,单击的单元格被聚焦,并且选择该行以实现DataGrid的单击编辑。它运作得很好。
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?
但我还希望我的DataGrid在更改单元格值时自动退出编辑模式(不按Enter键)。例如,在编辑模式下,我在单元格中有一个组合框。当用户在组合框中选择一个值时,它将自动对所选值进行数据绑定。但是,用户仍然需要单击Enter以退出编辑模式。如何自动退出编辑模式?
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:
我已经尝试监听属性更改并调用DataGrid的CommitEdit函数以自动退出编辑模式。效果很好,这里是代码:
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.
但现在单击编辑功能对当前单元格不起作用。我必须先点击另一行才能使其正常工作。我想我想要的是当调用CommmitEdit时,它会自动选择一个不同的行。 (就像当你点击Enter时,它将进入下一行)任何建议的人?请告诉我如何执行此操作的代码。我的项目时间不多了。
Thanks for the help.
谢谢您的帮助。
1 个解决方案
#1
7
to flip from cell editing template back to cell template:
从单元格编辑模板切换回单元格模板:
dataGrid.CancelEdit();
#1
7
to flip from cell editing template back to cell template:
从单元格编辑模板切换回单元格模板:
dataGrid.CancelEdit();