【发布时间】:2014-09-27 14:08:57
【问题描述】:
如标题所述,我想在编辑模式下获取 selectedRow 的所有单元格。
到目前为止,我已经尝试获取 dataGrid 的当前单元格和当前行,如下面的代码中所述。
private void DataGrid_Edit_Click(object sender, RoutedEventArgs e)
{
int colIndex = 0;
int rowIndex = 0;
DependencyObject dep = (DependencyObject)e.OriginalSource;
while (dep != null && !(dep is DataGridCell))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridCell)
{
colIndex = ((DataGridCell)dep).Column.DisplayIndex;
while (dep != null && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
DataGridRow row = (DataGridRow)dep;
rowIndex = FindRowIndex(row);
}
while (dep != null && !(dep is DataGrid))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
DataGrid dg = (DataGrid)dep;
for (int column = 0; column < colIndex; column++)
{
dg.CurrentCell = new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[column]);
dg.BeginEdit();
}
}
上面的代码就像它遍历指定行的所有单元格一样工作。但最后最后一个单元格进入编辑模式,所以我无法让以前的单元格处于编辑模式。
您能否建议对 selectedRow 的所有单元格而不是我得到的最后一个单元格执行相同的操作?
【问题讨论】:
-
循环遍历选定行的所有单元格并将单元格的
IsEditing属性设置为true。 -
@RohitVats 我错过了。我不知道 DataGridCell 上有任何名为 IsEditing 的属性。相反,我正在设置 DataGrid 的当前单元格。谢谢你。现在可以正常使用了。
-
太好了,我已将其转换为答案,以便它也可以帮助其他人。