Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义RichTextBox控件_C#_Silverlight_Windows Phone 7_Silverlight 4.0 - Fatal编程技术网

C# 自定义RichTextBox控件

C# 自定义RichTextBox控件,c#,silverlight,windows-phone-7,silverlight-4.0,C#,Silverlight,Windows Phone 7,Silverlight 4.0,在我的Windows Phone应用程序中,我使用RichTextBox控件。我有很长的文本,所以标准控件只显示其中的一部分 在互联网上我找到了这个解决方案:-这可以帮助我。它分离块并为每个文本块创建一个文本块。但是我怎样才能为RichTextBox做到这一点呢?是否可能,因为我的案例中的RichTextBox包含很多块?您可以通过在stackpanel或scrollviewer中添加多个RichTextBox控件来解决此问题。在添加每个文本块时,需要计算RichTextBox的大小。当大小的高

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


在互联网上我找到了这个解决方案:-这可以帮助我。它分离块并为每个文本块创建一个文本块。但是我怎样才能为RichTextBox做到这一点呢?是否可能,因为我的案例中的RichTextBox包含很多块?

您可以通过在stackpanel或scrollviewer中添加多个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()方法

私有void ProcessTextLength(字符串故事) { 字符串storytext=story.Replace(“\n\n”,“\n\n^”); List storylist=storytext.Split('^').ToList(); List finalstorylist=新列表(); 字符串currenttext=“”; foreach(故事列表中的变量项) { currenttext=this.StorytextBlock.Text; this.StorytextBlock.Text=this.StorytextBlock.Text+项; 如果(this.StorytextBlock.ActualHeight>2048) { 添加(当前文本); this.StorytextBlock.Text=项; } if(storylist.IndexOf(item)=storylist.Count-1) { 添加(this.StorytextBlock.Text); } } this.StorytextBlock.Text=“”; foreach(finalstorylist中的var finalitem) { 字符串文本=最终文本; if(text.StartsWith(“\n\n”)) text=text.子字符串(2); if(text.EndsWith(“\n\n”)) text=text.Remove(text.Length-2); this.textBlockStackPanel.Children.Add(新建TextBlock { 最大高度=2048, TextWrapping=TextWrapping.Wrap, FontSize=24, textTriming=textTriming.WordEllipsis, FontFamily=新FontFamily(“Segoe WP”), Text=Text, 前景=新的SolidColorBrush(Color.FromArgb(255,70,70,70)) }); } } 这将解决您的问题。如果这真的对你有帮助,请记为答案

谢谢
Kamal。

您可以通过在stackpanel或scrollviewer中添加多个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()方法

私有void ProcessTextLength(字符串故事) { 字符串storytext=story.Replace(“\n\n”,“\n\n^”); List storylist=storytext.Split('^').ToList(); List finalstorylist=新列表(); 字符串currenttext=“”; foreach(故事列表中的变量项) { currenttext=this.StorytextBlock.Text; this.StorytextBlock.Text=this.StorytextBlock.Text+项; 如果(this.StorytextBlock.ActualHeight>2048) { 添加(当前文本); this.StorytextBlock.Text=项; } if(storylist.IndexOf(item)=storylist.Count-1) { 添加(this.StorytextBlock.Text); } } this.StorytextBlock.Text=“”; foreach(finalstorylist中的var finalitem) { 字符串文本=最终文本; if(text.StartsWith(“\n\n”)) text=text.子字符串(2); if(text.EndsWith(“\n\n”)) text=text.Remove(text.Length-2); this.textBlockStackPanel.Children.Add(新建TextBlock { 最大高度=2048, TextWrapping=TextWrapping.Wrap, FontSize=24, textTriming=textTriming.WordEllipsis, FontFamily=新FontFamily(“Segoe WP”), Text=Text, 前景=新的SolidColorBrush(Color.FromArgb(255,70,70,70)) }); } } 这将解决您的问题。如果这真的对你有帮助,请标记为答案