重置 WPF Datagrid 滚动条位置
当更改 Datagrid 的 .DataContext 属性(更改为新源)时,所选项目将被清除,但滚动条位置将保留。为了避免这种情况,我在更改数据上下文后调用 .ScrollIntoView(.Item(0) 来向上移动滚动条。但它会在几分之一秒内显示错误的页面,当我在更改数据上下文之前滚动到顶部时,我有同样的问题,
那么如何同时更改 .DataContext 并重置滚动条位置?
编辑:我应该提到我的 XAML 看起来像这样:
<DataGrid VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
所以也许虚拟化是原因。
When changing the .DataContext property of a Datagrid (to a new source) the selected item gets cleared, but the scrollbar position is retained. To avoid this I call .ScrollIntoView(.Item(0), after changing the datacontext, to move the scrollbar upwards. But it displays the wrong page for a fraction of a second, and when I scroll to the top before changing the datacontext, i have the same problem.
So how can I change the .DataContext and resetting the scrollbar position at the same time?
EDIT: I should mention that my XAML looks like this:
<DataGrid VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
So maybe the virtualizing is the cause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在 DataContextChanged 事件中为
ScrollViewer
调用ScrollToTop
?获取VisualChild
Have you tried calling
ScrollToTop
for theScrollViewer
in the DataContextChanged event?GetVisualChild