How to use ScrollViewer.ScrollToVerticalOffset?

后端 未结 1 1463
我在风中等你
我在风中等你 2020-12-01 22:17

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 Wind

相关标签:
1条回答
  • 2020-12-01 23:07

    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!

    0 讨论(0)
提交回复
热议问题