在Windows 8应用程序中滚动RichTextBlock

时间:2014-05-19 05:19:48

标签: windows windows-8 winrt-xaml

我在ScrollViewer中有一个RichTextBlock。 RichTextBlock的内容很长。如何在RichTextBlock中滚动这些文本? 我希望在富文本块中实现滚动,因为文本在文本块中附加。这里的输入是通过文本框&给出的。文本框的那些文本被附加到富文本块中。

  

<Grid Background="{StaticResource
> ApplicationPageBackgroundThemeBrush}">
>                 <Grid.RowDefinitions>
>                     <RowDefinition Height="0.6*" />
>                     <RowDefinition Height="0.4*" />
>                 </Grid.RowDefinitions>
>         
>                 <Grid Grid.Row="0"
>                       Width="1200"
>                       Height="350"
>                       Background="#FFFF">
>                     <ScrollViewer x:Name="scrollViewerChat"
>                                   Width="1200"
>                                   Height="350"
>                                   HorizontalScrollBarVisibility="Disabled"
>                                   HorizontalScrollMode="Disabled"
>                                   VerticalScrollBarVisibility="Visible"
>                                   VerticalScrollMode="Auto">
>                         <RichTextBlock x:Name="rchChatBlock"
>                                        Grid.Row="0"
>                                        Width="1200"
>                                        Height="350"
>                                        FontSize="40"
>                                        Foreground="Black"
>                                        ScrollViewer.VerticalScrollBarVisibility="Visible">
>                             <Paragraph x:Name="rchParagraph" ScrollViewer.VerticalScrollBarVisibility="Visible">
>                                 <Run />
>                             </Paragraph>
>                         </RichTextBlock>
>                     </ScrollViewer>
>                 </Grid>
>         
>                 <TextBox x:Name="txtBoxChat"
>                          Grid.Row="1"
>                          Width="1200"
>                          Height="100"
>                          FontSize="40"
>                          />
>                 <Button Grid.Row="1"
>                         Width="150"
>                         Height="50"
>                         HorizontalAlignment="Left
>                         VerticalAlignment="Bottom"
>                         Click="btnSend_Click"
>                         Content="Send" />
>             </Grid>
>     
>     
>     
>     
>             private void btnSend_Click(object sender, RoutedEventArgs e)
>                     {
>                         LoadData2();            
>                     }
>         
>             private void LoadData2()
>                 {
>                     string input = txtBoxChat.Text;
>                     string pattern = " ";
>                     string[] substrings = Regex.Split(input, pattern);
>                     for (var i = 0; i < substrings.Length; i++)
>                     {
>                             var item = substrings[i];                                
>                             Run run1 = new Run();
>                             Run run2 = new Run();
>                             run1.Text = item;
>                             rchParagraph.Inlines.Add(run1);
>         
>                             string k = " ";
>                             run2.Text = k;
>                             rchParagraph.Inlines.Add(run2);                                   
>         
>                     }
>                     Run run3 = new Run();
>                     run3.Text = "\n";
>                     rchParagraph.Inlines.Add(run3);
>                     
>                 }

1 个答案:

答案 0 :(得分:0)

我不确定你想要使用什么事件,但我会让你决定。然后这样做:

public void CauseScroll()
{
    var box = new RichTextBlock();
    var scroll = Children(box).First(x => x is ScrollViewer) as ScrollViewer;
    scroll.ChangeView(null, scroll.ScrollableHeight, null);
}

public static IEnumerable<FrameworkElement> Children(FrameworkElement element)
{
    Func<DependencyObject, List<FrameworkElement>> recurseChildren = null;
    recurseChildren = (parent) =>
    {
        var list = new List<FrameworkElement>();
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            if (child is FrameworkElement)
                list.Add(child as FrameworkElement);
            list.AddRange(recurseChildren(child));
        }
        return list;
    };
    var children = recurseChildren(element);
    return children;
}

这将滚动到底部。

祝你好运!