简体   繁体   中英

Custom RichTextBox Control

In my Windows Phone application I use a RichTextBox Control. I have very long text, so the standard control displays only a part of it.

On the internet I have found this solution: Creating-scrollable-Textblock-for-WP7 - that can help me. It separate blocks and creates a TextBlock for each of the text blocks. But how can I do this for a RichTextBox? Is it possible, because the RichTextBox in my case contains a lot of blocks?

You can resolve this by adding multiple RichTextBox control inside a stackpanel or scrollviewer. You need to calculate the size of the RichTextBox while adding the each blocks of text. When ever the size seems to be exceeding above 2048 pixel in height/width you need to add the text in new Rich Textblock.

Find the below sample code for TextBlock implemented in the same way.

Step 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>

Step 2:

Just invoke the ProcessTextLength() method while loading the page.

private void ProcessTextLength(string story)
        {
            string storytext = story.Replace("\n\n", "\n\n^");
            List storylist = storytext.Split('^').ToList();
            List finalstorylist = new List();
            string currenttext = "";
            foreach (var item in storylist)
            {
                currenttext = this.StorytextBlock.Text;
                this.StorytextBlock.Text = this.StorytextBlock.Text + item;
                if(this.StorytextBlock.ActualHeight > 2048)
                {
                    finalstorylist.Add(currenttext);
                    this.StorytextBlock.Text = item;
                }
                if (storylist.IndexOf(item) == storylist.Count - 1)
                {
                    finalstorylist.Add(this.StorytextBlock.Text);
                }
            }
            this.StorytextBlock.Text = "";
            foreach (var finalitem in finalstorylist)
            {
                string text = finalitem;
                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
                                                          {
                                                              MaxHeight = 2048,
                                                              TextWrapping = TextWrapping.Wrap,
                                                              FontSize = 24,
                                                              TextTrimming = TextTrimming.WordEllipsis,
                                                              FontFamily = new FontFamily("Segoe WP"),
                                                              Text = text,
                                                              Foreground = new SolidColorBrush(Color.FromArgb(255,70,70,70))                                                             
                                                          });                
            }       
        }

This will resolve your issue. Please mark as answer if this really helps you.

Thanks Kamal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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