滚动到 ScrollViewer 结尾的最佳方式 [英] Best Way to Scroll to End of ScrollViewer
本文介绍了滚动到 ScrollViewer 结尾的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,当我添加新项目时,我让代码自动滚动到末尾的唯一方法如下:
Currently, the only way I have gotten my code to auto scroll to the end when I add a new item is the following:
XAML:
<ScrollViewer x:Name="chatViewScroller" HorizontalAlignment="Left" Height="201" Margin="0,32,0,0" VerticalAlignment="Top" Width="475" Background="#7FFFFFFF">
<StackPanel x:Name="chatViewContent" />
</ScrollViewer>
代码:
chatViewContent.Children.Add(
new TextBlock() {
Text = text,
FontSize = 18,
TextWrapping = Windows.UI.Xaml.TextWrapping.Wrap,
Margin = new Thickness(10, 3, 10, 0),
Foreground = new Windows.UI.Xaml.Media.SolidColorBrush(
isServerMessage ? Windows.UI.Colors.Purple : Windows.UI.Colors.Black)
});
await Task.Delay(10);
chatViewScroller.ScrollToVerticalOffset(chatViewScroller.ScrollableHeight);
这是公认的做法吗?我是否必须等待一些随机的时间?
Is this the accepted way of doing it? Do I have to wait for some random period of time?
推荐答案
使用 ActualHeight 对我不起作用(我还没有弄清楚原因) - 但是像这样使用 ScrollableHeight 就成功了:
Using ActualHeight did not work for me (I have yet to figure out why) - but using ScrollableHeight like this did the the trick:
// adding item to ItemsControl...
// ...
_scrollViewer.UpdateLayout();
_scrollViewer.ScrollToVerticalOffset(_scrollViewer.ScrollableHeight);
这篇关于滚动到 ScrollViewer 结尾的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文