wpf 更改列表视图中的确切滚动位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21442567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing exact scroll-position in a Listview
提问by Tokfrans
Been searching around the net for the answer but I haven't found anything that does this:
一直在网上搜索答案,但我没有找到任何这样做的东西:
I want to change the exact position in a WPF-list iew programmatically. Some way of saying
我想以编程方式更改 WPF 列表视图中的确切位置。某种说法
ListView.Scrollposition.Y = some value;
The only thing I can find is to change the value to a object within the Listview, not specific coordinates. If anyone has some good articles or posts in this subject, I'd be very grateful!
我唯一能找到的是将值更改为 Listview 中的对象,而不是特定坐标。如果有人在这个主题上有一些好的文章或帖子,我将不胜感激!
Thanks.
谢谢。
回答by Sheridan
There is no way of doing that using the ListViewelement. Instead, you need to access its ScrollViewerand then you can use the ScrollViewer.ScrollToVerticalOffsetMethodto set the vertical position of the ScrollViewer. You'll also need to use the ScrollViewer.VerticalOffset, ScrollViewer.VerticalOffset, ScrollViewer.ViewportHeightand ScrollViewer.ExtentHeightproperties to gauge where you are in the ScrollViewer.
使用ListView元素无法做到这一点。相反,您需要访问它ScrollViewer,然后您可以使用ScrollViewer.ScrollToVerticalOffset方法来设置ScrollViewer. 您还需要使用ScrollViewer.VerticalOffset,ScrollViewer.VerticalOffset,ScrollViewer.ViewportHeight和ScrollViewer.ExtentHeight性能来衡量你在哪里的ScrollViewer。
From the ScrollViewerClasspage on MSDN:
从MSDN 上的ScrollViewer类页面:
The area that includes all of the content of the ScrollViewer is the extent. The visible area of the content is the viewport.
包含 ScrollViewer 的所有内容的区域就是范围。内容的可见区域是视口。
Finally, how do you get the ScrollViewerfrom the ListView? I can't guarantee that this will work on a ListView, but it does on a ListBox. you can use the VisualTreeHelper.GetChildMethodto delve into the visual tree of the ListViewand it should contain a Borderand then the ScrollViewer, so you shouldbe able to do something like this:
最后,你如何ScrollViewer从ListView? 我不能保证这对 a 有效ListView,但在ListBox. 您可以使用VisualTreeHelper.GetChildMethod深入研究 the 的可视化树,ListView它应该包含 aBorder和 the ScrollViewer,因此您应该能够执行以下操作:
Border border = (Border)VisualTreeHelper.GetChild(YourListView, 0);
ScrollViewer scrollViewer = VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
if (scrollViewer != null)
{
scrollViewer.ScrollToVerticalOffset(60.0);
}
If you doget an error with what the GetChildmethod returns, it'll be easy to adjust by debugging it. Just put a breakpoint there and see what each child type is and add another line with one of those elements... eventually, it should find the ScrollViewer. However, I think that that code should be fine.
如果您确实收到该GetChild方法返回的错误,则可以通过调试轻松进行调整。只需在那里放置一个断点,看看每个子类型是什么,然后添加另一行包含其中一个元素的行......最终,它应该找到ScrollViewer. 但是,我认为该代码应该没问题。
回答by dkozl
If it's a ListViewthen first you'll need to find ScrollViewer:
如果是ListView然后首先你需要找到ScrollViewer:
private ScrollViewer FindScrollViewer(DependencyObject d)
{
if (d is ScrollViewer)
return d as ScrollViewer;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)
{
var sw = FindScrollViewer(VisualTreeHelper.GetChild(d, i));
if (sw != null) return sw;
}
return null;
}
and when you find it then you can ScrollToVerticalOffset
当你找到它时,你就可以 ScrollToVerticalOffset
var sw = FindScrollViewer(listView);
if (sw != null) sw.ScrollToVerticalOffset(x);

