在WPF DataGrid中使用Enter键作为选项卡 [英] Using Enter Key as Tab in WPF DataGrid

查看:155
本文介绍了在WPF DataGrid中使用Enter键作为选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中有一个 DataGrid

当我点击输入,当达到LastColumn时,它应该具有创建或移动到下一行的默认的 Enter 功能。

I want to move to the NextCell when i hit Enter and when the LastColumn is reached it should have the default Enter function of creating or moving to the next row.

我不想使用 Tab

我如何在WPF中执行此操作。

How can i do this in WPF.

推荐答案

尝试这个我认为它至少为我工作。

try this i think it work at least worked for me .

//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;
    }
}

在这段代码当一个单元格得到焦点和用户密钥下一个单元格将获得关注。
祝你好运希望能帮到你。

in this code when a cell got focus and user key down on that next cell will get focus. Good luck hope this help you.

编辑:

将此函数设置为datagrid PreviewKeyDown事件

Set this function as datagrid PreviewKeyDown event.

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;
    }

这篇关于在WPF DataGrid中使用Enter键作为选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆