WPF DataGrid - 如何自动退出编辑模式?

ncf*_*ion 12 wpf datagrid c#-4.0

我已经从Codeplex 实现了WPF DataGrid 单击编辑.在该解决方案中,单击单元格并选择行以实现DataGrid的单击编辑.它运作得很好.

这是代码:

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;
                    }
                }
            }
        }
    }    
Run Code Online (Sandbox Code Playgroud)

但我还希望我的DataGrid在更改单元格值时自动退出编辑模式(不按Enter键).例如,在编辑模式下,我在单元格中有一个组合框.当用户在组合框中选择一个值时,它将自动对所选值进行数据绑定.但是,用户仍然需要单击Enter以退出编辑模式.如何自动退出编辑模式?

我已经尝试侦听属性更改并调用DataGrid的CommitEdit函数以自动退出编辑模式.效果很好,这里是代码:

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

    }
Run Code Online (Sandbox Code Playgroud)

但现在单击编辑功能对当前单元格不起作用.我必须先点击另一行才能使其正常工作.我想我想要的是当调用CommmitEdit时,它会自动选择一个不同的行.(就像当你点击Enter时,它将进入下一行)任何建议的人?请告诉我如何执行此操作的代码.我的项目时间不多了.

谢谢您的帮助.

den*_*zov 9

从单元格编辑模板切换回单元格模板:

dataGrid.CancelEdit();
Run Code Online (Sandbox Code Playgroud)