Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 338
Vishal Avatar asked Apr 30 '14 16:04

Vishal


2 Answers

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();
        }
    }
}
like image 151
AsitK Avatar answered Sep 23 '22 12:09

AsitK


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;
  }
}
like image 28
javi Avatar answered Sep 22 '22 12:09

javi