views:

60

answers:

2

Hey guys, I want to move the listbox scrollbar to the bottom whenever a new item is added to the itemssource, but ScrollIntoView() doesn't seem to do anything if I pass it either a reference to the newly added item, or the index of it. Has anyone gotten this to work, or have any other suggestions as to how I could scroll the listbox down to the bottom?

Some code:

    void Actions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        //if a new item was added, set it to the selected item
        if (e.NewItems != null)
        {
            MainListBox.SelectedIndex = e.NewStartingIndex;

            //MainListBox.ScrollIntoView(MainListBox.Items.Last());     //todo: this doesnt seem to work
        }
    }
A: 

ScrollIntoView definitely works. I just added an application button to an empty databound app and doing the following on button click caused the list to scroll.

MainListBox.ScrollIntoView(MainListBox.Items.Last());

Could be an issue with an event on selectionChanged? Do you have anything hooked up to that?
Does the ScrollIntoView work if you don't set the selected item?

Matt Lacey
Hi Matt, where does the .Last() method come from? I add a new item to my collection and am not aware of the index - I don't have this method on my Items class?
Rodney
@Rodney `.Last()` is an extension method on IEnumerable<T> in System.Linq
Matt Lacey
Ah yes, thanks!
Rodney
+2  A: 

MSDN says:

When the contents of the ItemsSource collection changes, particularly if many items are added to or removed from the collection, you may need to call UpdateLayout() prior to calling ScrollIntoView for the specified item to scroll into the viewport.

Could that be your problem?

Jac
Perfect! This fixed it. Thanks heaps :)
Blakomen