Focus on DataGridCell for SelectedItem when DataGrid Receives Keyboard Focus

后端 未结 3 1254
南笙
南笙 2020-12-30 11:19

I have a DataGrid where the SelectedItem is bound to a VM Selected property. I have a search control that will do a find and the SelectedItem

3条回答
  •  青春惊慌失措
    2020-12-30 11:51

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

提交回复
热议问题