Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改键盘输入的默认行为_C#_Wpf_Datagrid_Wpfdatagrid - Fatal编程技术网

C# 更改键盘输入的默认行为

C# 更改键盘输入的默认行为,c#,wpf,datagrid,wpfdatagrid,C#,Wpf,Datagrid,Wpfdatagrid,我想禁用WPFDataGridUI控件的默认行为,即在特定单元格上按enter按钮时,焦点自动移动到下一个单元格。它应该只提交已编辑的新数据,而不移动到下一个单元格 我通过安装一个PreviewKeyDown处理程序并使用两个MoveFocus调用找到了解决方法。如果没有该解决方法(仅使用e.Handled=true语句),编辑的数据将无法正确提交(单元格将无限期地处于编辑模式) XAML: 有人能帮我找到更好的解决方案吗?调用committedit()方法提交数据: private void

我想禁用WPF
DataGrid
UI控件的默认行为,即在特定单元格上按enter按钮时,焦点自动移动到下一个单元格。它应该只提交已编辑的新数据,而不移动到下一个单元格

我通过安装一个
PreviewKeyDown
处理程序并使用两个
MoveFocus
调用找到了解决方法。如果没有该解决方法(仅使用
e.Handled=true
语句),编辑的数据将无法正确提交(单元格将无限期地处于编辑模式)

XAML:

有人能帮我找到更好的解决方案吗?

调用
committedit()
方法提交数据:

private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        e.Handled = true;
        ((DataGrid)sender).CommitEdit();
    }
}
    private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        var uiElement = e.OriginalSource as UIElement;
        if (e.Key == Key.Enter && uiElement != null)
        {
            e.Handled = true;
            uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
            uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
        }
    }
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        e.Handled = true;
        ((DataGrid)sender).CommitEdit();
    }
}