WPF DataGrid – How to automatically exit Edit Mode

c#-4.0datagridwpf

I have implemented WPF DataGrid Single-Click Editing from Codeplex.
In that solution, the clicked cell is focused and the row is selected to achieve
single-click editing of DataGrid. It worked great.

Here's the code:

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DataGridCell cell = sender as DataGridCell;
        if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
        {
            if (!cell.IsFocused)
            {
                cell.Focus();
            }
            DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
            if (dataGrid != null)
            {
                if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                {
                    if (!cell.IsSelected)
                        cell.IsSelected = true;
                }
                else
                {
                    DataGridRow row = FindVisualParent<DataGridRow>(cell);
                    if (row != null && !row.IsSelected)
                    {
                        row.IsSelected = true;
                    }
                }
            }
        }
    }    

But I also want my DataGrid to automatically exit editing mode (without hitting Enter key)
when a cell value is changed. For example, I have a combobox in the cell when in edit mode. When user selects a value in combobox, it will automatically databind the selected value. But then the user still need to click Enter to exit edit mode. How can I exit edit mode automatically?

I've tried listening for property changes and call CommitEdit function of DataGrid to exit edit mode automatically. Works great and here's the code:

 void _gameCompareViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "End Edit")
        {
            AlignGrid.CommitEdit();
        }

    }

But now the Single-Click editing feature will not work for the current cell.
I have to click a different row first in order for it to work.
I think what I want is when CommmitEdit is called, it automatically selects a
different row. (Like when you hit Enter, it will go to the next row)
Any suggestions guys? Please show me codes on how to do this. Im running out of time here for my project.

Thanks for the help.

Best Answer

to flip from cell editing template back to cell template:

dataGrid.CancelEdit();