在WPF DataGrid中使用回车键作为Tab键

6

我在WPF中有一个DataGrid

当我按下Enter时,我想要移动到下一个单元格,并且当达到最后一列时,它应该具有默认的Enter功能,即创建或移动到下一行。

我不想使用Tab键。

在WPF中如何实现这个功能?


你尝试过什么?听起来很简单,只需要侦听 KeyUp 事件并处理 <ENTER> 按键即可... - Tejs
dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); dgrow.移动焦点(new TraversalRequest(FocusNavigationDirection.Next)); - Kishore Kumar
我再次检查了我的答案,没有任何问题,只需进行以下两个更改:1-将datagrid的选择模式更改为Cell;2-在窗口顶部使用此引用using System.Windows.Controls.Primitives,并不要忘记将事件设置为datagrid视图的got focus。 - Mamad RN
2个回答

0

试试这个,我认为它有效,至少对我有效。

//datagrid gotfocus event
private void dataGrid1_GotFocus(object sender, RoutedEventArgs e)
{
    DependencyObject dep = (DependencyObject)e.OriginalSource;
    //here we just find the cell got focused ...
    //then we can use the cell key down or key up
    // iteratively traverse the visual tree
    while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    if (dep == null)
        return;

    if (dep is DataGridCell)
    {
        DataGridCell cell = dep as DataGridCell;
        //raise key down event of cell
        cell.IsSelected = true;
        cell.KeyDown += new KeyEventHandler(cell_KeyDown);
    }
}

void cell_KeyDown(object sender, KeyEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    if (e.Key == Key.Enter)
    {
        cell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        cell.IsSelected = false;
        e.Handled = true;
        cell.KeyDown -= cell_KeyDown;
    }
}

在这段代码中,当一个单元格获得焦点并且用户按下键盘时,下一个单元格将获得焦点。 祝好运,希望这对你有所帮助。
编辑:
将此函数设置为数据网格的PreviewKeyDown事件。
private void maindg_PreviewKeyDown(object sender, KeyEventArgs e)
        {

            //just accept enter key
            if (e.Key != Key.Enter) return;

        DependencyObject dep = (DependencyObject)e.OriginalSource;
        //here we just find the cell got focused ...
        //then we can use the cell key down or key up
        // iteratively traverse the visual tree
        while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }

        if (dep == null)
            return;

        if (dep is DataGridCell)
        {
            //cancel if datagrid in edit mode
            maindg.CancelEdit();
            //get current cell
            DataGridCell cell = dep as DataGridCell;
            //deselect current cell
            cell.IsSelected = false;
            //find next right cell
            var nextCell = cell.PredictFocus(FocusNavigationDirection.Right);
            //if next right cell null go for find next ro first cell
            if (nextCell == null)
            {
                DependencyObject nextRowCell;
                nextRowCell = cell.PredictFocus(FocusNavigationDirection.Down);
                //if next row is null so we have no more row Return;
                if (nextRowCell == null) return;
                //we do this because we cant use FocusNavigationDirection.Next for function PredictFocus
                //so we have to find it this way
                while ((nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left) != null)
                    nextRowCell = (nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left);
                //set new cell as next cell
                nextCell = nextRowCell;
            }
            //change current cell
            maindg.CurrentCell = new DataGridCellInfo(nextCell as DataGridCell);
            //change selected cell
            (nextCell as DataGridCell).IsSelected = true;
            // start edit mode
            maindg.BeginEdit();
        }
        //handl the default action of keydown
        e.Handled = true;
    }

@Mamad R,它能用,我有一个要求,当下一个单元格获得焦点时,如何使其可编辑 - Mussammil
@Mussammil,请查看我的最后一次编辑。希望能够帮到您 :) - Mamad RN
@MamadR 我尝试在我的示例项目中使用您的代码。我遇到了两个问题:1)数据网格的选择单元始终需要是CellOrRowHeader。2)当我在单元格上按Enter键时,焦点会移动到下一个单元格。但是我在单元格中输入的数据被删除了。 - Vishal

-1
一个更简单的实现方式。思路是捕获按键事件,如果按键是“Enter”,则移动到下一个选项卡,即网格中的下一个单元格。
/// <summary>
/// On Enter Key, it tabs to into next cell.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGrid_OnPreviewKeyDown(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.Next));
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接