如何在 Windows 8 Metro App 中获取 GridView.Selected Item 的滚动位置 [英] How to get GridView.Selected Item's Scroll Position in Windows 8 Metro App

查看:16
本文介绍了如何在 Windows 8 Metro App 中获取 GridView.Selected Item 的滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代码选择 gridview 项目,所以我还需要我的 gridview 在所选项目的位置滚动,我尝试了 GridView.ScrollintoPosition() 但它不起作用.有什么方法可以获取 SelectedItem 的 滚动位置,以便我可以使用 scrollViewer1.ScrollToHorizo​​ntalOffsetWithAnimation()

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()

推荐答案

这里有几个方面.

  1. 我认为只有 gridView.ScrollIntoView(gridView.SelectedItem) 应该可以工作.它有点异步,因此代码不会立即看到它滚动,但是如果您执行类似 await Task.Delay(100) 的操作 - 您可能会看到 ScrollViewer偏移量更新..
  2. 如果您想要动画滚动 - 您可以使用 WinRT XAML 工具包的 ScrollViewer.ScrollToHorizo​​ntalOffsetWithAnimation() 扩展,或者如果您的目标是 Windows 8.1 - 您可以使用新的 ScrollViewer.ChangeView() 支持动画滚动的方法.
  1. 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 like await Task.Delay(100) - you might be able to see the ScrollViewer offset updated..
  2. 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 new ScrollViewer.ChangeView() method that supports animated scroll.
  1. 首先需要获取GridView模板中ScrollViewer的实例.您可以使用 GetTemplatePart()VisualTreeHelper 来完成.
  2. 现在需要在ScrollViewer中获取SelectedItem的UI容器的位置.要做到这一点,您首先需要容器本身,您可以使用 var container = gv.ContainerFromItem(gv.SelectedItem) 获得它,但是如果 ItemsPanelGridView 是虚拟化的 - 您可能无法这样做,因为 SelectedItem 可能没有其 UI 容器.在这种情况下,出于多种原因,我会简单地进行非动画滚动 - 主要是性能,但如果您真的必须这样做 - 您可能能够根据 SelectedItem 中的索引计算位置项目和项目大小的集合,但可能有点复杂.
  3. 一旦你得到了容器,你就可以用类似var horizo​​ntalOffset = gridViewItem.TransformToVisual(scrollViewer).TransformPoint(new Point()).X;
  4. 此时您应该可以使用您喜欢的方法滚动到偏移量.
  1. You need to get the instance of the ScrollViewer in the GridView template first. You can either do it using GetTemplatePart() or with the VisualTreeHelper.
  2. Now you need to get the position of the UI container of the SelectedItem in the ScrollViewer. To do that you first need the container itself, which you can get using var container = gv.ContainerFromItem(gv.SelectedItem), but if the ItemsPanel of the GridView is virtualized - you might not be able to do that because the SelectedItem 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 the SelectedItem in the collection of items and item size, but it might be a bit complicated.
  3. Once you get the container you can get its position with something like var horizontalOffset = gridViewItem.TransformToVisual(scrollViewer).TransformPoint(new Point()).X;
  4. 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屋!

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