Is there an easy way for ListView to automatically scroll through the last added item without having to write code in code?

I have a ListView that is associated with an observable collection. Because items are added to the monitored collection, the list does not automatically scroll to show the last item added. I am trying to stick to good WPF practices and would like to avoid writing code in code. Is there an easy way to do this through XAML or the corresponding model code?

<ListView HorizontalAlignment="Center" ItemsSource="{Binding ScenarioSnippets}" Background="{x:Null}" 
                      BorderBrush="{x:Null}" BorderThickness="0" SelectionMode="Single" VerticalAlignment="Top" 
                      HorizontalContentAlignment="Center" IsSynchronizedWithCurrentItem="True">
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="Focusable" Value="False" />
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView Selector.IsSelected="True" AllowsColumnReorder="False">
                        <GridView.Columns>
                            <GridViewColumn CellTemplate="{StaticResource ScenarioSnippetItemCellTemplate}" 
                                            HeaderContainerStyle="{StaticResource GridViewHeaderStyle}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
        </ListView>
+5
source share
2 answers

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)

+9

Attached Behavior, ListView MVVM, .

/ ListBox ( a ListView) .

+2

All Articles