滚动到滚动视图中选定的 Treeviewitem
我有一个滚动查看器包装树视图。
我以编程方式填充树视图(未绑定),并将树视图扩展到预定的树视图项。一切都很好。
我的问题是,当树展开时,我希望作为树视图父级的滚动视图滚动到我刚刚展开的树视图项。有什么想法吗? - 请记住,树视图每次展开时可能不会具有相同的结构,因此排除了仅存储当前滚动位置并重置为该位置的情况......
I have a scrollviewer wrapping a treeview.
I populate the treeview programmatically (it is not bound), and expand the treeview to a predetermined treeviewitem. That all works fine.
My problem is that when the tree expands I would like the scrollview that parents the treeview to scroll to the treeviewitem that I just expanded. Any ideas? - keep in mind that the treeview may not have the same structure each time it is expanded, so that rules out just storing the current scroll position and reseting to that...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,TreeView 没有滚动到所选项目。
我所做的是,在将树展开到选定的 TreeViewItem 后,我调用 Dispatcher Helper 方法来允许 UI 更新,然后在选定的项目上使用 TransformToAncestor 来查找其在 ScrollViewer 中的位置。这是代码:
这是 DispatcherHelper 代码:
I've had the same issue, with the TreeView not scrolling to the selected item.
What I did was, after expanding the tree to the selected TreeViewItem, I called a Dispatcher Helper method to allow the UI to update, and then used the TransformToAncestor on the selected item, to find its position within the ScrollViewer. Here is the code:
Here is the DispatcherHelper code:
Jason 的 ScrollViewer 技巧是将 TreeViewItem 移动到特定位置的好方法。
但有一个问题:在 MVVM 中,您无权访问视图模型中的 ScrollViewer。无论如何,这是一种实现它的方法。如果你有一个 TreeViewItem,你可以沿着它的可视化树向上走,直到到达嵌入的 ScrollViewer:
Jason's ScrollViewer trick is a great way of moving a TreeViewItem to a specific position.
One problem, though: in MVVM you do not have access to the ScrollViewer in the view model. Here is a way to get to it anyway. If you have a TreeViewItem, you can walk up its visual tree until you reach the embedded ScrollViewer: