简体   繁体   English

XAML WP8并使“ TextBlock”向下滚动

[英]XAML WP8 and making a 'TextBlock' scroll down

Alright, so I have a XAML page with a TextBlock in a Windows Phone 8 application. 好的,所以我在Windows Phone 8应用程序中有一个带有TextBlock的XAML页面。 My dilemma is this: 我的困境是:

I pragmatically add more content (formatted lines with Inlines.Add(new Run...) to the TextBlock. The text block is currently filled from bottom to up because of the ScrollViewer in the sense that a line appears in the bottom after another. I would also be fine with them starting to appear from the top as long as the TextBlock would continue to scroll down (actually this might look better) once it is full. My TextBlock is inside a ScrollViewer as below: 我实用地向TextBlock添加更多内容(带有Inlines.Add(new Run ...)的带格式的行。由于ScrollViewer的原因,文本块当前从下到上填充,这意味着在一行中一行又一行地出现在底部。我也可以让他们从顶部开始出现,只要TextBlock填满就可以继续向下滚动(实际上看起来会更好),我的TextBlock在ScrollViewer中,如下所示:

    <Popup x:Name="send_message_xaml" Grid.Row="1" VerticalOffset="0" Width="750" Height="auto">
        <StackPanel Orientation="Vertical" Margin="0,0" Width="auto" Height="auto">
            <ScrollViewer Height="345" HorizontalAlignment="Left" Margin="0,0,0,0" Name="scrollViewer1" VerticalAlignment="Bottom" Width="420" VerticalScrollBarVisibility="Auto">
                <TextBlock x:Name="message_log"  Margin="40,50,0,0" TextWrapping="Wrap" Width="420" VerticalAlignment="Top" FontSize="22"/>
            </ScrollViewer>
            <TextBox x:Name="message_to_send" InputScope="Chat" Width="480" KeyDown="message_to_send_KeyDown" Margin="15,0"/>
        </StackPanel>
    </Popup>

How can I get the textblock to scroll so that the newest appended message is always at the bottom? 如何使文本块滚动,以使最新的附加消息始终位于底部? I found a bunch of these threads but none seem to solve my problem so far. 我发现了很多这样的线程,但到目前为止,似乎都没有一个能解决我的问题。 Do I need to add some code somewhere with the appending? 我是否需要在附加位置添加一些代码?

You need to update the VerticalOffset based on the ScrollableHeight . 您需要基于ScrollableHeight更新VerticalOffset When you add new inlines to the TextBlock , its height is going to change and that will notify the parent ScrollViewer . 当您向TextBlock添加新的内联时,其高度将改变,这将通知父级ScrollViewer So, after you add new items to the inlines, run the Measure method and update the VerticalOffset . 因此,在将新项添加到内联之后,请运行Measure方法并更新VerticalOffset

Here is an example. 这是一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM