Focus on DataGridCell for SelectedItem when DataGrid Receives Keyboard Focus

依然范特西╮ 提交于 2019-11-30 07:30:31
Richard E

You need to give the newly selected row logical focus. After selecting the new item try replacing your SetFocus call with this:

        var selectedRow = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(dataGrid1.SelectedIndex);
        FocusManager.SetIsFocusScope(selectedRow, true);
        FocusManager.SetFocusedElement(selectedRow, selectedRow);

This PowerShell snippet worked for me:

$dataGrid = ...    
$dataGrid.add_GotKeyboardFocus({
    param($Sender,$EventArgs)
    if ($EventArgs.OldFocus -isnot [System.Windows.Controls.DataGridCell) {
        $row = $dataGrid.ItemContainerGenerator.ContainerFromIndex($dataGrid.SelectedIndex)
        $row.MoveFocus((New-Object System.Windows.Input.TraversalRequest("Next")))
    }
})

The FocusManager solution didn't work for me for some reason. Also I required a more general apporach. So here is, what I came up with:

using System.Windows.Controls;

public static void RestoreFocus(this DataGrid dataGrid,
                                     int column = 0, bool scrollIntoView = false)
{
    if (dataGrid.IsKeyboardFocusWithin && dataGrid.SelectedItem != null)
    {
        // make sure everything is up to date
        dataGrid.UpdateLayout();

        if (scrollIntoView)
        {
            dataGrid.ScrollIntoView(dataGrid.SelectedItem);
        }

        var cellcontent = dataGrid.Columns[column].GetCellContent(dataGrid.SelectedItem);
        var cell = cellcontent?.Parent as DataGridCell;
        if (cell != null)
        {
            cell.Focus();
        }
    }
}

And call it like this:

MyDataGrid.IsKeyboardFocusWithinChanged += (sender, e) =>
{
    if ((bool)e.NewValue == true)
    {
        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, new Action(() =>
        {
            MyDataGrid.RestoreFocus(scrollIntoView: true);
        }));
    }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!