14

How can I programmatically force a silverlight list box to scroll to the bottom so that the last item added is always visible.

I've tried simply selecting the item. It ends up as selected but still not visible unless you manually scroll to it.

1
  • +1 for pointing out how Silverlight left out the most basic feature on a ListBox I can imagine. WinForms has had it forever! Shame you are actually not a member at the moment :) Commented Sep 27, 2011 at 11:36

4 Answers 4

24

Use the ListBox's ScrollIntoView method passing in the last item. You may need to call UpdateLayout immediately before it for it to work.

3
  • 1
    This is exactly the answer I was looking for except... I can't get it to work. It seems like this should work... If lst.Items.Count > 0 Then lst.SelectedIndex = lst.Items.Count - 1 lst.ScrollIntoView(lst.SelectedItem) lst.UpdateLayout() Else The last item is selected but not in view.
    – Joe Griffith
    Commented Jun 1, 2009 at 17:00
  • 1
    Call UpdateLayout BEFORE ScrollIntoView, hopefully that will work for you.
    – Bill Reiss
    Commented Jun 3, 2009 at 1:04
  • That did the trick. Works exactly as I wanted now. Thank you.
    – Joe Griffith
    Commented Jun 3, 2009 at 17:08
7

The ScrollIntoView() method will scroll the last item into view, however listBox.UpdateLayout() must be called just before ScrollIntoView(). Here is a complete method with code:

    // note that I am programming Silverlight on Windows Phone 7

    public void AddItemAndScrollToBottom(string message)
    {
        string timestamp = DateTime.Now.ToString("mm:ss");
        var item = new ListBoxItem();
        item.Content = string.Format("{0} {1}", timestamp, message);
        // note that when I added a string directly to the listbox, and tried to call ScrollIntoView() it did not work, but when I add the string to a ListBoxItem first, that worked great
        listBoxEvents.Items.Add(item);

        if (listBoxEvents.Items.Count > 0)
        {
            listBoxEvents.UpdateLayout();
            var itemLast = (ListBoxItem)listBoxEvents.Items[listBoxEvents.Items.Count - 1];
            listBoxEvents.UpdateLayout();
            listBoxEvents.ScrollIntoView(itemLast);
        }
    }
0
4

Slightly refactored to reduce the lines of code:

 listBoxEvents.Add(item)
 listBoxEvents.UpdateLayout()
 listBoxEvents.ScrollIntoView(listBoxEvents.Items(listBoxEvents.Items.Count - 1))
0

Just went through this and none of the solutions above worked in a Silverlight 5 app. The solution turned out to be this:

 public void ScrollSelectedItemIntoView(object item)
 {
      if (item != null)
      {                
          FrameworkElement frameworkElement = listbox.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;
          if (frameworkElement != null)
          {
              var scrollHost = listbox.GetScrollHost();                    
              scrollHost.ScrollIntoView(frameworkElement);
          }
      }                
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.