I have a DataGrid in which I have two columns namely Name
and Group
.
I have used EnterKeyTravarsal
Class, so that I can use Enter as TAB.
Also Based on some Conditions I add a new row to DataGrid in the ViewModel using InputBindings.
But when a new row is added, I can see that the last cell that was focused just before adding a new row remains in the edit mode.
Here is a sample project that I have created to clearly explain my problem.
Update your class EnterKeyTraversal
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();
}
}
}
I don't have the time to download all your code but i'll try to help.
I remember i had a similar problem and i solved it using either
this.grid.CommitEdit();
or
this.grid.CommitEdit(DataGridEditingUnit.Row, true);
in your case you might need yo use "DataGridEditingUnit.Cell"
I call the methods before inserting the new line, but I remember I had lots of problems to get them work and finally didn't get to understand them properly (they are working fine though), sorry :S
EDIT: Code por public DataGrid
public DataGrid MyGrid
{
get
{
return this.grid;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With