Reputation: 6360
I'm trying to implement automatic scrolling of a Listview using MVVM. I know that you can update the listview by calling ScrollIntoView but that requires Code Behind which I'm trying to avoid.
I've bound my ListView's ItemSource to an ObservableCollection and would like the Listview to automatically scroll down to the newest item added to the log.
<ListView ItemsSource="{Binding Log}"
SelectedIndex="{Binding SelectedLine}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Grid.Row="1">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel>
</VirtualizingStackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Type" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type}" HorizontalAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Upvotes: 2
Views: 3337
Reputation: 19895
MVVM
and Attached Behaviors
go hand in hand. You can use attached behavior to scroll to new item after the ObservableCollection.Add()
takes place...
This article is a good example that tries to bring a tree view item into scroll view while sticking to MVVM.
Upvotes: 2