什麼是您設置爲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