You can use the Blend behavior:
public class AutoScrollToLastItemBehavior : Behavior<ListBox>
{
protected override void OnAttached()
{
base.OnAttached();
var collection = AssociatedObject.Items.SourceCollection as INotifyCollectionChanged;
if (collection != null)
collection.CollectionChanged += collection_CollectionChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
var collection = AssociatedObject.Items.SourceCollection as INotifyCollectionChanged;
if (collection != null)
collection.CollectionChanged -= collection_CollectionChanged;
}
private void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
ScrollToLastItem();
}
}
private void ScrollToLastItem()
{
int count = AssociatedObject.Items.Count;
if (count > 0)
{
var last = AssociatedObject.Items[count - 1];
AssociatedObject.ScrollIntoView(last);
}
}
}
XAML:
<ListView ItemsSource="...">
<i:Interaction.Behaviors>
<local:AutoScrollToLastItemBehavior />
</i:Interaction.Behaviors>
</ListView>
( Behaviorand classes Interactioncan be found in System.Windows.Interactivity.dllthe SDK Blend)