如何在 Windows 8 Metro App 中获取 GridView.Selected Item 的滚动位置 [英] How to get GridView.Selected Item's Scroll Position in Windows 8 Metro App
本文介绍了如何在 Windows 8 Metro App 中获取 GridView.Selected Item 的滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用代码选择 gridview 项目,所以我还需要我的 gridview 在所选项目的位置滚动,我尝试了 GridView.ScrollintoPosition() 但它不起作用.有什么方法可以获取 SelectedItem 的 滚动位置,以便我可以使用 scrollViewer1.ScrollToHorizontalOffsetWithAnimation()
I am selecting gridview item using code, so I also need my gridview to Scrolls at selected item's position , I tried GridView.ScrollintoPosition() but it is not working . IS there any way to get the Scroll position of SelectedItem so that I can scroll it using scrollViewer1.ScrollToHorizontalOffsetWithAnimation()
推荐答案
这里有几个方面.
- 我认为只有
gridView.ScrollIntoView(gridView.SelectedItem)
应该可以工作.它有点异步,因此代码不会立即看到它滚动,但是如果您执行类似await Task.Delay(100)
的操作 - 您可能会看到ScrollViewer代码>偏移量更新..
- 如果您想要动画滚动 - 您可以使用 WinRT XAML 工具包的
ScrollViewer.ScrollToHorizontalOffsetWithAnimation()
扩展,或者如果您的目标是 Windows 8.1 - 您可以使用新的ScrollViewer.ChangeView()
支持动画滚动的方法.
- I think just
gridView.ScrollIntoView(gridView.SelectedItem)
should work. It's a bit asynchronous, so the code wouldn't immediately see it scrolled, but if you do something likeawait Task.Delay(100)
- you might be able to see theScrollViewer
offset updated.. - If you want an animated scroll - you can use WinRT XAML Toolkit's
ScrollViewer.ScrollToHorizontalOffsetWithAnimation()
extension or if you are targeting Windows 8.1 - you can use the newScrollViewer.ChangeView()
method that supports animated scroll.
- 首先需要获取
GridView
模板中ScrollViewer
的实例.您可以使用GetTemplatePart()
或VisualTreeHelper
来完成. - 现在需要在
ScrollViewer
中获取SelectedItem
的UI容器的位置.要做到这一点,您首先需要容器本身,您可以使用var container = gv.ContainerFromItem(gv.SelectedItem)
获得它,但是如果的
是虚拟化的 - 您可能无法这样做,因为ItemsPanel
GridViewSelectedItem
可能没有其 UI 容器.在这种情况下,出于多种原因,我会简单地进行非动画滚动 - 主要是性能,但如果您真的必须这样做 - 您可能能够根据SelectedItem
中的索引计算位置项目和项目大小的集合,但可能有点复杂. - 一旦你得到了容器,你就可以用类似
var horizontalOffset = gridViewItem.TransformToVisual(scrollViewer).TransformPoint(new Point()).X;
- 此时您应该可以使用您喜欢的方法滚动到偏移量.
- You need to get the instance of the
ScrollViewer
in theGridView
template first. You can either do it usingGetTemplatePart()
or with theVisualTreeHelper
. - Now you need to get the position of the UI container of the
SelectedItem
in theScrollViewer
. To do that you first need the container itself, which you can get usingvar container = gv.ContainerFromItem(gv.SelectedItem)
, but if theItemsPanel
of theGridView
is virtualized - you might not be able to do that because theSelectedItem
might not have its UI container. I would simply do the non-animated scroll in that case for many reasons - mainly performance, but if you really have to - you might be able to calculate the position based on the index of theSelectedItem
in the collection of items and item size, but it might be a bit complicated. - Once you get the container you can get its position with something like
var horizontalOffset = gridViewItem.TransformToVisual(scrollViewer).TransformPoint(new Point()).X;
- At this point you should be able to scroll to the offset using the method you like.
这篇关于如何在 Windows 8 Metro App 中获取 GridView.Selected Item 的滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文