How to use ScrollViewer.ScrollToVerticalOffset?

江枫思渺然 提交于 2019-11-27 09:19:57

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

First, get the transform between the TextBox and ScrollViewer.

GeneralTransform transform = textBox.TransformToVisual(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));

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

scrollViewer.ScrollToVerticalOffset(textBoxPosition.Y);

Good luck!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!