R – How to BringToView the latest item added to a bound ObservableCollection

silverlight

I have a silverlight control (View) which displays a list of items in a specified property of the datacontext (viewmodel).

What I need is for the scrollviewer in my control to scroll to the top or bottom depending on where the latest item has been added to the list. (It'll always be either the beginning or the end of the list, I don't need to worry about middle of list insertions.)

In WPF i'd just use the DataContextChanged event to start listening to the viewmodel, but in silverlight that event is internal.

Any ideas on how to tackle this?

Best Answer

A good starting point is Attached Behaviors on CodeProject.

A useful behavior would watch the ListBox.ItemsSource and attach to the observable collection when set. On the collection changed event, use ListBox.ScrollIntoView to display the changed item.