WPF DataGrid添加新行设置焦点第一个单元格

WPF DataGrid添加新行设置焦点第一个单元格,wpf,datagrid,focus,Wpf,Datagrid,Focus,WPF DataGrid在添加新行焦点时始终设置为单元格的最后位置 在添加新行时,如何设置第一个单元格的焦点 1.我有简单的6列,所以当我在最后一列按enter键时,它应该添加新行(工作正常) 2.焦点应该是添加行的第一个单元格,它不会发生,它总是在最后一个单元格中 我附上我的WPF样品演示以及请纠正我哪里错了? 演示链接: 谢谢,, Jitendra Jadav.您可以在数据网格上处理previewkeydown: private void dg_PreviewKeyDown(object s

WPF DataGrid在添加新行焦点时始终设置为单元格的最后位置

在添加新行时,如何设置第一个单元格的焦点

1.我有简单的6列,所以当我在最后一列按enter键时,它应该添加新行(工作正常) 2.焦点应该是添加行的第一个单元格,它不会发生,它总是在最后一个单元格中

我附上我的WPF样品演示以及请纠正我哪里错了? 演示链接:

谢谢,,
Jitendra Jadav.

您可以在数据网格上处理previewkeydown:

private void dg_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var el = e.OriginalSource as UIElement;
    if (e.Key == Key.Enter && el != null)
    {
        e.Handled = true;
        el.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
}
标记可能很明显,但:

    <DataGrid Name="dg"
              ...
              PreviewKeyDown="dg_PreviewKeyDown"

您可以处理
CellEditEnding
事件,并获得对
DataGridCell
的引用,如以下博文所述

如何在WPF中以编程方式选择和聚焦数据网格中的行或单元格:

这似乎对我有用:

private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(CollectionView.NewItemPlaceholder) as DataGridRow;
    if (row != null)
    {
        dataGrid.SelectedItem = row.DataContext;
        DataGridCell cell = GetCell(dataGrid, row, 0);
        if (cell != null)
            dataGrid.CurrentCell = new DataGridCellInfo(cell);
    }
}

private static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
{
    if (rowContainer != null)
    {
        DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
        if (presenter != null)
            return presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
    }
    return null;
}

private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null && child is T)
            return (T)child;
        else
        {
            T childOfChild = FindVisualChild<T>(child);
            if (childOfChild != null)
                return childOfChild;
        }
    }
    return null;
}
private void dataGrid\u CellEditEnding(对象发送方,DataGridCellEditEndingEventArgs e)
{
DataGridRow行=dataGrid.ItemContainerGenerator.ContainerFromItem(CollectionView.NewItemPlaceholder)作为DataGridRow;
如果(行!=null)
{
dataGrid.SelectedItem=row.DataContext;
DataGridCell=GetCell(dataGrid,行,0);
如果(单元格!=null)
dataGrid.CurrentCell=新的DataGridCellInfo(单元格);
}
}
私有静态DataGridCell GetCell(DataGrid DataGrid,DataGridRow rowContainer,int列)
{
if(rowContainer!=null)
{
DataGridCellsPresenter=FindVisualChild(rowContainer);
如果(演示者!=null)
将presenter.ItemContainerGenerator.ContainerFromIndex(列)作为DataGridCell返回;
}
返回null;
}
私有静态T FindVisualChild(DependencyObject obj),其中T:DependencyObject
{
for(int i=0;i
让用户使用datagrid像excel一样输入数据通常是需要避免的。验证是一个特别的噩梦。谢谢你@Andy,但不幸的是你的建议不起作用。如果有任何数据提交给datagrid,我的建议肯定有效。恐怕我真的不想在这里下载并深入研究您的应用程序。谢谢您的建议,它可以正常工作,但在按第一个单元格的enter键时,只有一个问题是添加新行。我想在按最后一个单元格的enter键时添加新行。请更正一下,或者给我一些指导,这可能会很有帮助。这是一个完全不同的问题。您最初的问题是“在添加新行时如何设置第一个单元格的焦点?”我相信这个问题已经得到了回答。如果您有其他问题,请提出新问题。如果可以的话,我很乐意帮助你。但请不要在评论栏中问其他问题。您好@mm8,这是对的,但如果您看一下我提到的我的要求,请检查一下,如果您想问其他问题,那么我会发布新问题,请让我知道。如前所述,您最初的问题是添加新行时如何聚焦第一个单元格。再一次,如果你有其他问题,请问一个新问题。