如何在 WPF ListView 中查看最后添加的列表视图项
我正在使用视图模型绑定到列表视图。每次我在视图模型内部可观察集合中添加一个项目时,我都会使用 list.Count-1 触发 LastIndex 属性。列表视图绑定到 VM 的 LastIndex 属性,并且列表视图正确选择添加到视图的最后一项。不幸的是,视图无法将最后添加的项目滚动到视图中。
我尝试在列表视图标记上设置 IsSynchronizedWithCurrentItem = "True",但没有帮助。
这是我正在使用的标记
<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>
任何这方面的帮助将不胜感激
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.
I tried setting IsSynchronizedWithCurrentItem = "True" on list view markup, but it did not help.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要调用 ScrollIntoView:
http://msdn .microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview.aspx
编辑:
这是在 XAML 中执行此操作的方法:
http://michlg.wordpress.com/2010/01/16/listbox-automatically-scroll-当前项目进入视图/
You need to call ScrollIntoView:
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview.aspx
EDIT:
And here's a way to do it in XAML:
http://michlg.wordpress.com/2010/01/16/listbox-automatically-scroll-currentitem-into-view/