我有一個列表框,添加了各種項目。當一個新項目被添加到列表框中時,我需要將該項目滾動到視圖中(基本上滾動到底部)。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,所以我很確定這不是問題。 –