小编Hai*_*bak的帖子

滚动WPF DataGrid以在顶部显示所选项目

我有一个包含许多项的DataGrid,我需要以编程方式滚动到SelectedItem.我在StackOverflow和Google上搜索过,似乎解决方案是ScrollIntoView,如下所示:

grid.ScrollIntoView(grid.SelectedItem)
Run Code Online (Sandbox Code Playgroud)

向上或向下滚动DataGrid,直到所选项目处于焦点.但是,根据相对于所选项目的当前滚动位置,所选项目可能最终成为DataGrid的ScrollViewer中的最后一个可见项目.我希望所选项目将成为ScrollViewer中的第一个可见项目(假设DataGrid中有足够的行允许这样做).所以我尝试了这个:

'FindVisualChild is a custom extension method that searches in the visual tree and returns 
'the first element of the specified type
Dim sv = grid.FindVisualChild(Of ScrollViewer)
If sv IsNot Nothing Then sv.ScrollToEnd()
grid.ScrollIntoView(grid.SelectedItem)
Run Code Online (Sandbox Code Playgroud)

首先,我滚动到DataGrid的末尾,然后才滚动到SelectedItem,此时SelectedItem显示在DataGrid的顶部.

我的问题是滚动到DataGrid的末尾运行良好,但随后滚动到所选项目并不总是有效.

如何解决此问题,或者是否有其他替代策略可以滚动到顶部位置的特定记录?

vb.net wpf datagrid scrollviewer

10
推荐指数
2
解决办法
2005
查看次数

标签 统计

datagrid ×1

scrollviewer ×1

vb.net ×1

wpf ×1