locked
How to implement typewriter-like scrolling

    Question

  • I'm trying to work out how to implement typewriter-like-scrolling in a RichEditBox in a C# WinRT app. Basically, I want to keep the active line centered vertically in the middle of the screen at all times. When a new line is reached, the document should scroll upwards slightly so as to place this new line in the center of the screen.

    The following code (adapted from another question here) automatically scrolls the RichEditBox (named EditorBox) to the bottom:

    private void ScrollToCenter()
    {
        var grid = (Windows.UI.Xaml.Controls.Grid)Windows.UI.Xaml.Media.VisualTreeHelper.GetChild(EditorBox, 0);
        for (var i = 0; i <= Windows.UI.Xaml.Media.VisualTreeHelper.GetChildrenCount(grid) - 1; i++)
        {
            object obj = Windows.UI.Xaml.Media.VisualTreeHelper.GetChild(grid, i);
            if (!(obj is Windows.UI.Xaml.Controls.ScrollViewer)) continue;
            ((Windows.UI.Xaml.Controls.ScrollViewer)obj).ChangeView(null, ((Windows.UI.Xaml.Controls.ScrollViewer)obj).ExtentHeight, null);
            break;
        }
    }

    I think that this or something similar to this can be edited to do what I want, but I need to be able to correctly set the vertical offset, and I can't work out how to do this.

    How can I correctly determine and set the vertical offset to achieve this effect?

    Wednesday, June 04, 2014 2:08 PM

Answers

  • Hi MarcusAureliusll,

    Well, you are almost half way, the ScrollViewer.ChangeView() method is the one that can set the vertical offset, for instance you can calculate the height of the ScrollViewer content and scroll to Height/2 position.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, June 05, 2014 7:59 AM
    Moderator