ScrollViewer.EnsureVisible for Windows Phone

Posted by Daniel Moth on Daniel Moth See other posts from Daniel Moth or by Daniel Moth
Published on Sun, 31 Jul 2011 03:06:47 GMT Indexed on 2011/11/11 18:19 UTC
Read the original article Hit count: 725

Filed under:

In my Translator By Moth app, on both the current and saved pivot pages the need arose to programmatically scroll to the bottom. In the former, case it is when a translation takes place (if the text is too long, I want to scroll to the bottom of the translation so the user can focus on that, and not their input text for translation). In the latter case it was when a new translation is saved (it is added to the bottom of the list, so scrolling is required to make it visible). On both pages a ScrollViewer is used.

In my exploration of the APIs through intellisense and msdn I could not find a method that auto scrolled to the bottom. So I hacked together a solution where I added a blank textblock to the bottom of each page (within the ScrollViewer, but above the translated textblock and the saved list) and tried to make it scroll it into view from code. After searching the web I found a little algorithm that did most of what I wanted (sorry, I do not have the reference handy, but thank you whoever it was) that after minor tweaking I turned into an extension method for the ScrollViewer that is very easy to use:

	this.Scroller.EnsureVisible(this.BlankText);

The method itself I share with you here:

    public static void EnsureVisible(this System.Windows.Controls.ScrollViewer scroller, 
                                          System.Windows.UIElement uiElem)
    {
      System.Diagnostics.Debug.Assert(scroller != null);
      System.Diagnostics.Debug.Assert(uiElem != null);

      scroller.UpdateLayout();

      double maxScrollPos = scroller.ExtentHeight - scroller.ViewportHeight;
      double scrollPos = 
              scroller.VerticalOffset - 
              scroller.TransformToVisual(uiElem).Transform(new System.Windows.Point(0, 0)).Y;

      if (scrollPos > maxScrollPos) scrollPos = maxScrollPos;
      else if (scrollPos < 0) scrollPos = 0;

      scroller.ScrollToVerticalOffset(scrollPos);
    }

I am sure there are better ways, but this "worked for me" :-)




Comments about this post by Daniel Moth welcome at the original blog.

© Daniel Moth or respective owner

Related posts about MobileAndEmbedded

  • Translator by Moth v2

    as seen on Daniel Moth - Search for 'Daniel Moth'
    If you are looking for the full manual for this Windows Phone app you can find it here: "Translator by Moth". While the manual has no images (just text), in this post I will share images and if you like them, go get "Translator by Moth" from the Windows Phone marketplace. open the app from… >>> More

  • Windows Phone 7 developer resources

    as seen on Daniel Moth - Search for 'Daniel Moth'
    Developers of Windows Mobile 6.x (and indeed Windows CE) applications still use the rich .NET Compact Framework 3.5 with Visual Studio 2008 for development. That is still a great platform and the Mobile Development Handbook is still a useful resource (if I may say so myself :-). The release of Windows… >>> More

  • ScrollViewer.EnsureVisible for Windows Phone

    as seen on Daniel Moth - Search for 'Daniel Moth'
    In my Translator By Moth app, on both the current and saved pivot pages the need arose to programmatically scroll to the bottom. In the former, case it is when a translation takes place (if the text is too long, I want to scroll to the bottom of the translation so the user can focus on that, and not… >>> More

  • "Translator by Moth"

    as seen on Daniel Moth - Search for 'Daniel Moth'
    This article serves as the manual for the free Windows Phone 7 app called "Translator by Moth". The app is available from the following link (browse the link on your Window Phone 7 phone, or from your PC with zune software installed): http://social.zune.net/redirect?type=phoneApp&id=bcd09f8e-8211-e011-9264-00237de2db9e… >>> More

  • Watermark TextBox for Windows Phone

    as seen on Daniel Moth - Search for 'Daniel Moth'
    In my Translator by Moth app, in the textbox where the user enters a translation I show a "prompt" for the user that goes away when they tap on it to enter text (and returns if the textbox remains/becomes empty). See screenshot on the right (or download the free app to experience it). Back… >>> More