开发者

How to make ScrollViewer automatic

开发者 https://www.devze.com 2022-12-15 02:19 出处:网络
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. He

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

精彩评论

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