什么是您设置为DataGrid的SelectionUnit?这适用于SelectionUnit =“CellOrRowHeader”,否则有些事情在最后必须稍作修改。无论如何,下面的代码将一个单元放入编辑模式,然后提交它触发CellEditEnding事件。如果
public void EnterCellEditEndingForCell(DataGridColumn column, DataRowView dataRowView)
{
int selectedRowIndex = -1;
// c_dataGrid.Items.IndexOf(dataRowView)
// sometimes break and return -1.
for (int i = 0; i < c_dataGrid.Items.Count; i++)
{
DataRowView rowView = c_dataGrid.Items[i] as DataRowView;
if (rowView == dataRowView)
{
selectedRowIndex = i;
break;
}
}
int selectedColumnIndex = c_dataGrid.Columns.IndexOf(column);
if (selectedRowIndex >= 0 && selectedColumnIndex >= 0)
{
DataGridCell dataGridCell = DataGridHelper.GetCell(c_dataGrid, selectedRowIndex, selectedColumnIndex);
if (dataGridCell == null)
{
c_dataGrid.ScrollIntoView(dataRowView);
dataGridCell = DataGridHelper.GetCell(c_dataGrid, selectedRowIndex, selectedColumnIndex);
}
dataGridCell.BringIntoView();
dataGridCell.Focus();
c_dataGrid.SelectedCells.Clear();
DataGridCellInfo info = new DataGridCellInfo(dataGridCell);
c_dataGrid.SelectedCells.Add(info);
c_dataGrid.BeginEdit();
c_dataGrid.CommitEdit(DataGridEditingUnit.Cell, true);
}
}
DataGridHelper.cs你没有对它的一个实现
static class DataGridHelper
{
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
static public DataGridCell GetCell(DataGrid dg, int row, int column)
{
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
static public DataGridRow GetRow(DataGrid dg, int index)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
dg.ScrollIntoView(dg.Items[index]);
row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
}
UPDATE 如果您选择只有一个单元格(这听起来像的情况下),比你能做到这一点。
private void tb_Comments_KeyUp(object sender, KeyEventArgs e)
{
if (rowBeingEdited != null)
{
rowBeingEdited.Row["Comments"] = tb_Comments.Text;
DataGridColumn columnBeingEdited= GetActiveDataGridColumn();
EnterCellEditEndingForCell(columnBeingEdited, rowBeingEdited);
}
}
private DataGridColumn GetActiveDataGridColumn()
{
if (c_dataGrid.SelectedCells.Count != 1)
{
return null;
}
return c_dataGrid.SelectedCells[0].Column;
}
我创建了一个辅助类和介绍的方法EnterCellEditEndingForCell在我的代码..但我需要在XAML或Tb_Keyup方法调用此?我没有清楚地理解你什么时候在datagrid中的选择单元。当我在文本框中输入数据时,它使用“rowBeingEdited.Row [”Comments“] = tb_Comments.Text;在datagrid中写入数据”非常感谢你的帮助.. – prem 2010-10-24 20:39:12
另外我忘了提及我使用WPF,但你创建的帮助文件适用于windows窗体..我可以使用WPF也是如此吗?对不起,我是C#编码的初学者,并试图学习的东西..再次感谢帮助.. – prem 2010-10-24 20:55:56
当您在Xaml中设置DataGrid,什么是SelectionUnit设置?它可以是SelectionUnit =“CellOrRowHeader”,SelectionUnit =“Cell”或SelectionUnit =“FullRow”。这将改变SelectionCells,SelectedItem等的行为。是的,辅助类是WPF,你应该在代码中调用这个方法。我更新了我的示例 – 2010-10-24 21:14:51