0

在我的 Windows Phone 应用程序中,我使用 RichTextBox 控件。我的文本很长,所以标准控件只显示其中的一部分。

在互联网上,我找到了这个解决方案:Creating-scrollable-Textblock-for-WP7 - 这可以帮助我。它分隔块并为每个文本块创建一个 TextBlock。但是我怎样才能为 RichTextBox 做到这一点?有可能吗,因为我的情况下的 RichTextBox 包含很多块?

4

1 回答 1

3

您可以通过在堆栈面板或滚动查看器中添加多个 RichTextBox 控件来解决此问题。您需要在添加每个文本块时计算 RichTextBox 的大小。当大小似乎超过 2048 像素的高度/宽度时,您需要在新的富文本块中添加文本。

找到以下以相同方式实现的 TextBlock 示例代码。

第1步:

<pre>
<ScrollViewer Margin="10,0,0,70">              
<StackPanel Grid.Row="4" Margin="0,-36,12,12" x:Name="textBlockStackPanel">

<TextBlock x:Name="StorytextBlock" Margin="0,0,12,12" MaxHeight="2048" TextWrapping="Wrap" FontSize="24" TextTrimming="WordEllipsis"  FontFamily="Segoe WP" d:LayoutOverrides="Width"   Foreground="#FF464646"  />

</StackPanel>                       
</ScrollViewer>
</pre>

第2步:

只需在加载页面时调用 ProcessTextLength() 方法。

私人无效ProcessTextLength(字符串故事)
        {
            字符串 storytext = story.Replace("\n\n", "\n\n^");
            列出故事列表 = storytext.Split('^').ToList();
            列表 finalstorylist = new List();
            字符串当前文本 = "";
            foreach(故事列表中的 var 项目)
            {
                currenttext = this.StorytextBlock.Text;
                this.StorytextBlock.Text = this.StorytextBlock.Text + 项目;
                如果(this.StorytextBlock.ActualHeight > 2048)
                {
                    finalstorylist.Add(currenttext);
                    this.StorytextBlock.Text = 项目;
                }
                if (storylist.IndexOf(item) == storylist.Count - 1)
                {
                    finalstorylist.Add(this.StorytextBlock.Text);
                }
            }
            this.StorytextBlock.Text = "";
            foreach (var finalitem in finalstorylist)
            {
                字符串文本=最终项目;
                if (text.StartsWith("\n\n"))
                    text = text.Substring(2);
                if (text.EndsWith("\n\n"))
                    text = text.Remove(text.Length - 2);
                this.textBlockStackPanel.Children.Add(new TextBlock
                                                          {
                                                              最大高度 = 2048,
                                                              TextWrapping = TextWrapping.Wrap,
                                                              字体大小 = 24,
                                                              TextTrimming = TextTrimming.WordEllipsis,
                                                              FontFamily = new FontFamily("Segoe WP"),
                                                              文字=文字,
                                                              前景 = 新的 SolidColorBrush(Color.FromArgb(255,70,70,70))                                                             
                                                          });                
            }       
        }

这将解决您的问题。如果这真的对您有帮助,请标记为答案。

谢谢卡马尔。

于 2012-07-04T10:44:55.273 回答