WPF DataGrid adding new row setting focus first cell

人盡茶涼 提交于 2019-12-23 04:26:38

问题


WPF DataGrid while adding new row focus is always setting to last position of cell.

How would I can set to the first cell's focus while adding new row?

1.I have simple 6 columns so when I press enter on last column it should add new row (working fine) 2.Focus should be the added row's first cell it won't happen it is always in the last cell

I am attaching my WPF Sample demo as well please correct me where I am wrong? Demo Link: WPFDemo

Thank you, Jitendra Jadav.


回答1:


You could handle the CellEditEnding event and get a reference to the DataGridCell as explained in the following blog post.

How to programmatically select and focus a row or cell in a DataGrid in WPF: https://blog.magnusmontin.net/2013/11/08/how-to-programmatically-select-and-focus-a-row-or-cell-in-a-datagrid-in-wpf/

This seems to work for me:

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



回答2:


You could handle previewkeydown on the datagrid:

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

The markup is probably obvious but:

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

There may well be some unexpected side effects, I just tested you hit enter in the last cell and you end up in the first cell of the next row OK.



来源:https://stackoverflow.com/questions/49997387/wpf-datagrid-adding-new-row-setting-focus-first-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!