自定义 RichTextBox 控件 [英] Custom RichTextBox Control

查看:37
本文介绍了自定义 RichTextBox 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

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

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

第一步:

<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="宽度" 前景="#FF464646"/></StackPanel></ScrollViewer>

第 2 步:

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

<前>私有无效ProcessTextLength(字符串故事){string storytext = story.Replace("\n\n", "\n\n^");List storylist = storytext.Split('^').ToList();List finalstorylist = new List();字符串 currenttext = "";foreach(故事列表中的变量项目){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){字符串文本 = 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{最大高度 = 2048,TextWrapping = TextWrapping.Wrap,字体大小 = 24,TextTrimming = TextTrimming.WordEllipsis,FontFamily = new FontFamily("Segoe WP"),文字 = 文字,前景 = 新的 SolidColorBrush(Color.FromArgb(255,70,70,70))});}}

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

谢谢卡马尔.

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.

这篇关于自定义 RichTextBox 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆