如何使用 ScrollViewer.ScrollToVerticalOffset? [英] How to use ScrollViewer.ScrollToVerticalOffset?

查看:34
本文介绍了如何使用 ScrollViewer.ScrollToVerticalOffset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不是重复的,但我找不到关于如何实际使用ScrollToVerticalOffset()的任何文档或示例.我在 Windows Phone 8 应用程序中使用它,但我认为它仍然适用于 WP7 和 Silverlight(不过,如果我错了,请随时纠正我).

I hope this isn't a duplicate but I can't find any documentation or examples on how to actually use ScrollToVerticalOffset(). I'm using it in a Windows Phone 8 app, but I think it will still apply to WP7 and Silverlight (although, feel free to correct me if I'm wrong).

这是我的基本设置(来自内存的伪代码):

So here is my basic set up (pseudo-code from memory):

<phone.PivotItem>
   <ScrollViewer>
      <Grid Height="1500">
         <Grid.RowDefinitions>
            <!-- about 20 rows, all auto-height -->
         </Grid.RowDefinitions>

         <Border Grid.Row="0">
            <TextBox x:Name="txt1" />
         </Border>
         <Border Grid.Row="1">
            <TextBox x:Name="txt2" />
         </Border>

         <!-- ...... -->

         <Border Grid.Row="19">
            <TextBox x:Name="txt20" />
         </Border>
      </Grid>
   </ScrollViewer>
</phone.PivotItem>

如您所见,我在 PivotItem 中有一个 ScrollViewer,里面有一个 Grid.在 Grid 中有大约 20 个 TextBox,每个都在 Border 内.当此页面加载时,我将焦点动态设置为这些 TextBox 之一,因此无论何时我将焦点设置为 TextBox #6-20(大致) - 我必须手动滚动下来看看.我想自动滚动我的 ScrollViewer 以便任何 TextBox 获得焦点,它都会居中供用户查看.

So as you can see, I've got a ScrollViewer within a PivotItem, and inside is a Grid. In the Grid there are about 20 TextBoxs, each within a Border. I am dynamically setting focus to one of these TextBoxs when this page loads, so anytime I set focus to TextBox #6-20 (roughly) - I have to manually scroll down to see it. I want to auto-scroll my ScrollViewer so that whichever TextBox has focus, it will be centered for the user to see.

ScrollToVerticalOffset() 的文档 说:

The documentation for ScrollToVerticalOffset() says:

将 ScrollViewer 内的内容滚动到指定的位置垂直偏移位置.

Scrolls the content that is within the ScrollViewer to the specified vertical offset position.

并且它接受一种System.Double.

我不明白的是 A) 我应该传递的值,以及 B) 我如何才能获得该值?它应该是一个介于 0 和我的 Grid (1500) 高度之间的数字吗?如果是这样,我如何确定任何给定 TextBox 的位置以便我可以滚动到它?

What I don't understand is A) the value I'm supposed to pass, and B) how I could even get that value? Is it supposed to be a number between 0 and the height of my Grid (1500)? If so, how could I determine the position of any given TextBox so I can scroll to it?

如果有任何简单的例子,请随时链接到它们.我不确定在调用此方法时 ScrollViewer 中的内容是否重要,但如果确实如此,我想确切地显示我是如何使用它的.

If there are any straightforward examples, please feel free to link out to them. I'm not sure if the content within the ScrollViewer matters when calling this method, but in case it does I wanted to show exactly how I'm using it.

非常感谢!

推荐答案

您可以使用 UIElement.TransformToVisual 调用查看任何 UIElement 相对于另一个 UIElement 的位置.

You can see any UIElement's position relative to another UIElement using the UIElement.TransformToVisual call.

首先,获取 TextBox 和 ScrollViewer 之间的转换.

First, get the transform between the TextBox and ScrollViewer.

GeneralTransform transform = textBox.TransformToVisual(scrollViewer);

然后,找出 TextBox 的哪个点 (0,0) 相对于 ScrollViewer.意思是,TextBox 原点 (0,0) 位于 ScrollViewer 的哪个位置.

Then, figure out what point (0,0) of the TextBox is relative to the ScrollViewer. Meaning, the TextBox origin (0,0) is located at what ScrollViewer position.

Point textBoxPosition = transform.Transform(new Point(0, 0));

既然您知道了 TextBox 相对于 ScrollViewer 的 Y 位置,请滚动到该 Y 偏移量.

Now that you know the Y position of the TextBox relative to the ScrollViewer, scroll to that Y offset.

scrollViewer.ScrollToVerticalOffset(textBoxPosition.Y);

祝你好运!

这篇关于如何使用 ScrollViewer.ScrollToVerticalOffset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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