The cell remains in edit mode even after it loses focus in the wpf datagrid

I have a DataGrid in which I have two columns, namely Name and Group .

I used the EnterKeyTravarsal class, so I can use Enter as a TAB .

Also based on some conditions, I add a new row to the DataGrid in the ViewModel using InputBindings.

But when a new row is added, I see that the last cell that was focused just before adding a new row remains in edit mode.

Here is a sample project that I created to clearly explain my problem.

+8
c # wpf mvvm xaml datagrid
source share
2 answers

Update your EnterKeyTraversal class

 static void ue_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e) { var ue = e.OriginalSource as FrameworkElement; DataGrid dg = FindVisualParent<DataGrid>(ue) as DataGrid; DataGridCell cell = FindVisualParent<DataGridCell>(ue); if (e.Key == Key.Enter) { //e.Handled = true; if (dg != null) { dg.CommitEdit(); } if (cell != null) { cell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } else { ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } if (dg != null) { dg.BeginEdit(); } } } 
+2
source share

I donโ€™t have time to download all your code, but I will try to help.

I remember I had a similar problem and I decided to use it either

 this.grid.CommitEdit(); 

or

 this.grid.CommitEdit(DataGridEditingUnit.Row, true); 

in your case, you may need to use "DataGridEditingUnit.Cell"

I call methods before inserting a new line, but I remember that I had a lot of problems to make them work and finally did not understand them correctly (they work fine, though), sorry: S

EDIT: Code por public DataGrid

 public DataGrid MyGrid { get { return this.grid; } } 
+1
source share

All Articles