开发者

How to make ScrollViewer automatic

I tried to place a TextBlock inside a ScrollViewer, and the scroll bar shows up correctly, but I cannot seem to make it automatically scroll down when the Text property of the TextBlock is updated. Here's the开发者_JS百科 relevant part of the XAML:

<ScrollViewer>
  <TextBlock FontFamily="Consolas"
             Text="{Binding Current.Current.Discussion}"
             TextWrapping="Wrap" />
</ScrollViewer>

Help would be greatly appreciated, thanks!


By default, the behavior you get is that the scroll bars will adjust to the amount of text in the textblock, but the viewer will be showing the top of the text. To refresh that properly do this:

scrollViewer.UpdateLayout();
scrollViewer.ScrollToVerticalOffset(txtBlock.ActualHeight);


Listen to the text changed event

    textBlock.TextChanged += (o, args) => ScrollTextBoxToBotton();

Actual function to scroll to bottom:

    private void ScrollTextBoxToBotton()
    {
        scrollViewer.UpdateLayout();
        scrollViewer.ScrollToVerticalOffset(double.MaxValue);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜