WPF TreeView - 如何滚动使展开的分支可见 [英] WPF TreeView - How to scroll so expanded branch is visible

查看:76
本文介绍了WPF TreeView - 如何滚动使展开的分支可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在树视图中展开项目以便需要滚动时,会出现一个滚动条.但是,它不会为新扩展的项目分支向下滚动 - 它们被控件底部裁剪.因此,当我继续扩展树底部的项目时,我必须继续手动向下滚动以查看新的子项.有人对如何让它自动滚动以显示新扩展的项目有建议吗?

When I expand items in my treeview so that scrolling is necessary, a scrollbar appears. However, it doesn't scroll down for the newly expanded branch of items - they get cropped by the bottom of the control. So as I continue expanding items at the bottom of the tree, I have to keep manually scrolling down to see the new children. Anyone have a suggestion for how make it automatically scroll to show the newly expanded items?

推荐答案

在 TreeView 上,处理 TreeViewItem.Expanded 事件(由于事件冒泡,您可以在 TreeView 级别执行此操作).在 Expanded 处理程序中,在引发事件的 TreeViewItem 上调用BringIntoView.

On the TreeView, handle the TreeViewItem.Expanded event (you can do this at the TreeView level because of event bubbling). In the Expanded handler, call BringIntoView on the TreeViewItem that raised the event.

您可能需要反复试验才能在事件处理程序代码中获取 TreeViewItem.我认为(尚未检查)您的 Expanded 事件处理程序的 sender 参数将是 TreeView(因为这是附加事件处理程序的位置)而不是 TreeViewItem.而 e.Source 或 e.OriginalSource 可能是 TreeViewItem 的数据模板中的一个元素.因此,您可能需要使用 VisualTreeHelper 沿着可视化树向上查找 TreeViewItem.但是,如果您使用调试器来检查发送方和 RoutedEventArgs,这应该很容易弄清楚.

You may need a bit of trial and error to get hold of the TreeViewItem in your event handler code. I think (haven't checked) that the sender argument to your Expanded event handler will be the TreeView (since that's where the event handler is attached) rather than the TreeViewItem. And the e.Source or e.OriginalSource may be an element in the TreeViewItem's data template. So you may need to use VisualTreeHelper to walk up the visual tree to find the TreeViewItem. But if you use the debugger to inspect the sender and the RoutedEventArgs this should be trivial to figure out.

(如果您能够使其正常工作并希望将其捆绑起来,以便不必将相同的事件处理程序附加到每个 TreeView,那么将其封装为 附加行为 这将允许您申请以声明方式,包括通过样式.)

(If you're able to get this working and want to bundle it up so you don't have to attach the same event handler to every TreeView, it should be easy to encapsulate it as an attached behaviour which will allow you to apply it declaratively, including via a Style.)

这篇关于WPF TreeView - 如何滚动使展开的分支可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆