[英]WPF: How to extract Scrollbar From ScrollViewer programmatically?
I'd like to access the Scrollbar from within my ScrollViewer. 我想从我的ScrollViewer中访问Scrollbar。
I think it's hidden somewhere within the ScrollViewer's template, is there a way for me to access, and get a reference to it programmatically? 我认为它隐藏在ScrollViewer模板中的某个地方,有没有办法让我访问,并以编程方式获取它的引用?
So if I have 所以,如果我有
<ScrollViewer x:Name="myScrollViewer">
In the code behind I'd like to go: 在我想要的代码中:
ScrollBar scrollBar = myScrollViewer.GetScrollBar();
(obviously, I assume it'd be trickier than just that) (显然,我认为它比那更棘手)
I think I got it.... 我想我明白了......
myScrollViewer.ApplyTemplate();
ScrollBar s = myScrollViewer.Template.FindName("PART_VerticalScrollBar", myScrollViewer) as ScrollBar;
You will need to use the VisualTreeHelper.GetChild
method to walk the visual tree of the ScrollViewer
to find the ScrollBar
. 您需要使用VisualTreeHelper.GetChild
方法遍历ScrollViewer
的可视树以查找ScrollBar
。
Since this method provides very low-level functionality and using it in high-level code will be painful, you will probably want to utilize a wrapper like LINQ to visual tree . 由于此方法提供了非常低级的功能,并且在高级代码中使用它会很痛苦,您可能希望利用LINQ这样的包装器来实现可视化树 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.