在Sliverlight或者WPF程序中,与Textbox相比,RichTextBox提供更为强大的功能,例如支持多种文本格式,支持图文混派,内嵌控件等等,而Windows Phone在升级到Mango(7.1)后也开始支援这个控件。现在还只是Beta版,所以在功能上还有所欠缺:
只读,还不能输入编辑;
Tool box中还没有添加这个控件,只能手工创建;
没有默认样式,所以得自定义样式文件;
没有Design View实时支持。
转自:http://www.cnblogs.com/wpdev/archive/2011/09/30/2196215.html
![](https://pic002.cnblogs.com/images/2011/276364/2011093010513787.png)
在Sliverlight或者WPF程序中,与Textbox相比,RichTextBox提供更为强大的功能,例如支持多种文本格式,支持图文混派,内嵌控件等等,而Windows Phone在升级到Mango(7.1)后也开始支援这个控件。现在还只是Beta版,所以在功能上还有所欠缺:
- 只读,还不能输入编辑;
- Tool box中还没有添加这个控件,只能手工创建;
- 没有默认样式,所以得自定义样式文件;
- 没有Design View实时支持。
手动创建RichTextBox的方法有两种,一种是在XAML声明,如:
1 |
< RichTextBox x:Name = "rtxtBox" Margin = "10" VerticalAlignment = "Top" > |
2 |
< Paragraph FontSize = "30" > |
3 |
RichTextBox Demo Project |
另外一种是通过Code-Behide:
01 |
RichTextBox rtb = new RichTextBox(); |
03 |
rtb.Background = new SolidColorBrush(Colors.White); |
04 |
rtb.VerticalContentAlignment = System.Windows.VerticalAlignment.Top; |
06 |
Paragraph parag = new Paragraph(); |
08 |
run.Foreground = new SolidColorBrush(Colors.Red); |
09 |
run.Text = "Red Text" ; |
10 |
parag.Inlines.Add(run); |
11 |
rtb.Blocks.Add(parag); |
13 |
ContentPanel.Children.Add(rtb); |
这里要注意,正如前面提到的,RichTextBox没有默认的样式,需要手动添加,否则不能正常显示,在App.xaml内添加自定义的样式如下:
02 |
< Application.Resources > |
03 |
< Style TargetType = "RichTextBox" > |
04 |
< Setter Property = "FontSize" Value = "{StaticResource PhoneFontSizeNormal}" /> |
05 |
< Setter Property = "Foreground" Value = "{StaticResource PhoneForegroundBrush}" /> |
06 |
< Setter Property = "Background" Value = "Transparent" /> |
07 |
< Setter Property = "BorderBrush" Value = "Transparent" /> |
08 |
< Setter Property = "BorderThickness" Value = "0" /> |
09 |
< Setter Property = "HorizontalContentAlignment" Value = "Stretch" /> |
10 |
< Setter Property = "VerticalContentAlignment" Value = "Center" /> |
11 |
< Setter Property = "Padding" Value = "0" /> |
12 |
< Setter Property = "Template" > |
14 |
< ControlTemplate TargetType = "RichTextBox" > |
15 |
< Grid Background = "Transparent" > |
16 |
< Border Background = "{TemplateBinding Background}" BorderBrush = "{TemplateBinding BorderBrush}" BorderThickness = "{TemplateBinding BorderThickness}" Margin = "{StaticResource PhoneHorizontalMargin}" > |
17 |
< ContentControl x:Name = "ContentElement" HorizontalContentAlignment = "{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment = "{TemplateBinding VerticalContentAlignment}" Padding = "{TemplateBinding Padding}" /> |
24 |
</ Application.Resources > |
如果想在RichTextBox内嵌入控件,可定义如下:
03 |
< Button Content = "InlineButton" /> |
08 |
< Image Width = "80" Source = "ApplicationIcon.jpg" /> |
之前在做Silverlight应用时,写过一个自定义的RichTextBox控件,能够保存Html基本样式,在做鲜闻阅读器时,遇到的问题也是对于Html流的处理,现在写自定义的RichTextBox Control for Windows Phone,希望能实现自己想要的转化功能。
Microsoft视频教程