我有一个列表框,添加了各种项目。当一个新项目被添加到列表框中时,我需要将该项目滚动到视图中(基本上滚动到底部)。ListBox ScrollIntoView不适用于可变高度的项目
我从How can I have a ListBox auto-scroll when a new item is added?尝试了解决方案,也从this blog post
然而,无论是解决方案的工作,因为我的列表框包含可变高度的项目。如果我把我的列表框项目模板改为固定高度,那么它似乎工作。以下是我的一个项目模板的示例:
<DataTemplate x:Key="StatusMessageTemplate">
<Grid Grid.Column="1" VerticalAlignment="top" Margin="0,5,10,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Path=MessageText}" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" FontWeight="Bold" Foreground="{DynamicResource LightTextColorBrush}"/>
<TextBlock Text="{Binding Path=created_at, StringFormat=t}" Style="{StaticResource Timestamp}" TextWrapping="Wrap" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="1"/>
</Grid>
</DataTemplate>
如何让新项目滚动到视图中而不管其高度如何?
当你调用ScrollIntoView(),你确定你有你的列表框项目的顶级FrameworkElement的一个参考,而的不是一个一个辅助函数ListBoxItem的子控件?我不确定这是否会有所作为,但如果确实如此,它可能会完全符合你的要求。 – Steve
这两个班我尝试使用listitem,所以我很确定这不是问题。 –