如何在WPF ListView中查看最后添加的列表视图项

时间:2021-06-27 14:30:32

I am using a view model to bind to the list view. Every time I add an item in the view model internal observable collection, I trigger an LastIndex property with the list.Count-1. The list view is bound to this LastIndex proeprty of VM and the listview correctly selects the last item added to the view. Unfortunately, the view is not able to scroll the last added item into view.

我正在使用视图模型绑定到列表视图。每次我在视图模型内部可观察集合中添加一个项目时,我都会触发一个带有list.Count-1的LastIndex属性。列表视图绑定到VM的LastIndex属性,listview正确选择添加到视图的最后一项。不幸的是,视图无法将最后添加的项目滚动到视图中。

I tried setting IsSynchronizedWithCurrentItem = "True" on list view markup, but it did not help.

我尝试在列表视图标记上设置IsSynchronizedWithCurrentItem =“True”,但它没有帮助。

This is the markup I am 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 will be greatly appreciated

在这方面的任何帮助将不胜感激

1 个解决方案

#1


15  

You need to call ScrollIntoView:

你需要调用ScrollIntoView:

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

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

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

EDIT:

编辑:

And here's a way to do it in XAML:

这是在XAML中实现的一种方法:

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

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

#1


15  

You need to call ScrollIntoView:

你需要调用ScrollIntoView:

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

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

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

EDIT:

编辑:

And here's a way to do it in XAML:

这是在XAML中实现的一种方法:

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

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