How to use the last item in a viewlist in a WPF ListView

I am using a view model to bind to a list. Each time I add an item to the internal observable collection of the view model, I launch the LastIndex property with the list. Count-1. The list view is tied to this LastIndex function for the virtual machine, and listview correctly selects the last item added to the view. Unfortunately, the view cannot scroll the last added item in the view.

I tried setting IsSynchronizedWithCurrentItem = "True" in the markup of the list, but that didn't help.

This is the markup I'm using

<ListView ItemsSource="{Binding Path=Status.Messages}" SelectedIndex="{Binding Path=Status.LastIndex, Mode=OneWay}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" Height="60" IsSynchronizedWithCurrentItem="True" > <ListView.Resources> <Style TargetType="{x:Type GridViewColumnHeader}"> <Setter Property="Visibility" Value="Collapsed" /> </Style> </ListView.Resources> <ListView.View> <GridView AllowsColumnReorder="False" > <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Path=.}" FontWeight="Thin" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> <ListView. </ListView> 

Any help in this regard would be greatly appreciated.

+6
listview wpf listviewitem
source share
1 answer

You need to call ScrollIntoView:

 list.ScrollIntoView(list.Items[list.Items.Count - 1]); 

http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview.aspx

EDIT:

And here is a way to do it in XAML:

http://michlg.wordpress.com/2010/01/16/listbox-automatically-scroll-currentitem-into-view/

+15
source share

All Articles