要实现在 WPF 中选中 DataGrid 的一行后将其显示在相应的位置上,可以使用 ScrollIntoView() 方法。
例如,假设你的 DataGrid 的名称为 myDataGrid,当用户点击行时,你可以使用以下代码:
myDataGrid.ScrollIntoView(myDataGrid.SelectedItem);
这将把选定的行滚动到可见范围内。
如果你想让选定的单元格始终显示在 DataGrid 中间,可以使用以下代码:
myDataGrid.ScrollIntoView(myDataGrid.SelectedItem, myDataGrid.Columns[0]);
将 myDataGrid.Columns[0] 替换为要滚动到一行中的特定列。
希望这可以帮助你解决问题!