2014-11-24 98 views
0

假设我在DataGrid中有5列和5行。现在我想在编辑模式下获取currentRow的所有单元格。我成功地做到了。现在我想禁用除单元格正在编辑的行之外的所有行。但在下面的代码r.IsEditing永远不会返回true。有人可以解释我为什么吗?DataGridRow.IsEditing永不返回true

for (int column = 0; column <= dg.Columns.Count - 1; column++) 
{ 
    if (!(GetDataGridCell(new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[column])).IsReadOnly)) 
    { 
     GetDataGridCell(new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[column])).IsEditing = true; 
    } 
} 

foreach (DataGridRow r in rows) 
{ 
    if (!(r.IsEditing)) 
    { 
     r.IsEnabled = false; 
    } 
} 

回答

0

这是有点棘手。我需要下面的行添加到我的代码,使其工作:

dg.CurrentCell = new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[0]); 
dg.BeginEdit(); 

所以,现在我的完整代码如下所示:

dg.CurrentCell = new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[0]); 
dg.BeginEdit(); 

for (int column = 0; column <= dg.Columns.Count - 1; column++) 
{ 
    if (!(GetDataGridCell(new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[column])).IsReadOnly)) 
    { 
     GetDataGridCell(new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[column])).IsEditing = true; 
    } 
} 

foreach (DataGridRow r in rows) 
{ 
    if (!(r.IsEditing)) 
    { 
     r.IsEnabled = false; 
    } 
}