Wpf c#中的DataGrid单元格单击编辑模式?

我的wpf应用程序中有DataGrid。 这是一些事件。 我需要单击编辑datagrid单元格。 当我双击单元格时,它正在改变。 我已经采取了一些措施。 但是,它对我不起作用。 行和列不一致。 动态地,我将为Datagrid创建列。

这是我的代码..

XAML:

 

我曾经为“GotFocus”事件添加了一些代码。 但它不适合我。 任何帮助都会得到真正的重视。

CS:

  private void dgTest_GotFocus(object sender, RoutedEventArgs e) { // Lookup for the source to be DataGridCell if (e.OriginalSource.GetType() == typeof(DataGridCell)) { // Starts the Edit on the row; DataGrid grd = (DataGrid)sender; grd.BeginEdit(e); } } 

试试这个:

在DataGrid上连接PreviewMouseLeftButtonDown事件:

     

然后在代码背后:

  private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridCell cell = (DataGridCell) sender; if (cell != null && !cell.IsEditing && !cell.IsReadOnly) { if (!cell.IsFocused) { cell.Focus(); } if (grdData.SelectionUnit != DataGridSelectionUnit.FullRow) { if (!cell.IsSelected) { cell.IsSelected = true; } } else { DataGridRow row = FindVisualParent(cell); if (row != null && !row.IsSelected) { row.IsSelected = true; } } } } static T FindVisualParent(UIElement element) where T : UIElement { UIElement parent = element; while (parent != null) { T correctlyTyped = parent as T; if (correctlyTyped != null) { return correctlyTyped; } parent = VisualTreeHelper.GetParent(parent) as UIElement; } return null; }