《RichTextBox》——— RichTextBox的使用

      在这篇文章中我将讨论WP7的RichTextBox控制(仍处在测试阶段)。在Silverlight 4中RichTextBox是一个众所周知的控制,所以Windows Phone 7.1(芒果)也一样可供RichTextBox。
      基本上RichTextBox代表一个富文本编辑控件,支持格式化的文本、超链接、内联图像和其他丰富的内容。
 
      当前Beta测试版RichTextBox有相当多的局限性
 * 没有默认的样式:没有默认的样式RichTextBox。使用RichTextBox控制你必须在你工程的App.xaml文件中添加一个新的风格,作为一个
    StaticResource。
 * 没有工具箱支持:RichTextBox的控制并不会出现在工具箱
 * 没有设计支持:RichTextBox的控制并不呈现在设计图面。为了解决这一问题,运行您的应用程序,并验证控制体现。
 * RichTextBox的控制是只读的
 
      还请注意,超链接和InlineUIContainer并非在任何情况下都能正确工作。因为一些原因,超链接可能将随机的抛出一个异常。InlineUIContainer不使用正确的UIElement内容也是如此。 最后,总结,看来唯一能正常在Windows Phone 7 Beta(芒果)工作的是paragraph对象,Span groups(Bold, Italic, and Underline)和Run对象。
 
      下面的基本任务可以在RichTextBox (WP7芒果)控制中工作:
TaskImplementation
内容设置
使用Paragraph来进行内容设置
显示文本和行内 UIElement对象
使用  InlineUIContainer元素
显示超链接
使用Hyperlink
在只读模式显示内容使用 IsReadOnly属性

 

 
 
 
 
 
注意: 只有当RichTextBox是只读属性时,用户界面元素才会被激活。
 
要开始使用RichTextBox首先你必须添加一些默认的样式,你既可以在App.xaml中加,也可以作为一个StaticResource资源加在页面里。
Option1、在App.xaml的资源中定义RichTextBox的默认样式:
 1 <Application.Resources> 
 2     <Style TargetType="RichTextBox"> 
 3         <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" /> 
 4         <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" /> 
 5         <Setter Property="Background" Value="Transparent" /> 
 6         <Setter Property="BorderBrush" Value="Transparent" /> 
 7         <Setter Property="BorderThickness" Value="0"/> 
 8         <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
 9         <Setter Property="VerticalContentAlignment" Value="Center" /> 
10         <Setter Property="Padding" Value="0" /> 
11         <Setter Property="Template"> 
12             <Setter.Value> 
13                 <ControlTemplate TargetType="RichTextBox"> 
14                     <Grid Background="Transparent"> 
15                         <Border Background="{TemplateBinding Background}" 
16                     BorderBrush="{TemplateBinding BorderBrush}"
17                     BorderThickness="{TemplateBinding BorderThickness}"
18                     Margin="{StaticResource PhoneHorizontalMargin}"> 
19                             <ContentControl x:Name="ContentElement"
20                         HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
21                         VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
22                         Padding="{TemplateBinding Padding}"/> 
23                         </Border> 
24                     </Grid> 
25                 </ControlTemplate> 
26             </Setter.Value> 
27         </Setter> 
28     </Style> 
29 </Application.Resources>
示例用法:为了展示RichTextBox的使用,我们在RichTextBox中添加一些内容,如:
1 <RichTextBox Width="400" Height="200" Background="Transparent" BorderBrush="White" BorderThickness="5"> 
2     <Paragraph> 
3         <Run Text="A simple RichTextBox sample "/> 
4         <Bold Foreground="Red">Some bold Text Here!</Bold> 
5     </Paragraph> 
6 </RichTextBox>
 
Option2:在这种情况下,您可以在你的页面资源中定义RichTextBox的Style,加上一个“x:key”标识符,以便您可以使用StaticResource访问这个风格:
1 <phone:PhoneApplicationPage.Resources> 
2     <Style TargetType="RichTextBox" x:Key="rtbx"> 
3         .//the same style as described in Option1 , the only difference is x:Key     
4     </Style> 
5 </phone:PhoneApplicationPage.Resources>
示例用法:为了展示RichTextBox的使用,我们在RichTextBox中添加一些内容,如:
 
1 <RichTextBox Width="400" Height="400" Background="Transparent" BorderBrush="White" BorderThickness="5" Style="{StaticResource rtbx}"> 
2     <Paragraph> 
3         <Run Text="A simple RichTextBox"/> 
4         <Bold Foreground="Red">Some bold Text Here!</Bold> 
5     </Paragraph> 
6 </RichTextBox>

运行如下:

 RichTextBox使用样例:
 这个示例展示如何使用Paragraph, Run, Bold, Italic  和 Underline对象
 
 1 <RichTextBox  Width="400" Height="300" > 
 2     <Paragraph> 
 3         <Run Text="A simple RichTextBox: "/> 
 4         <Italic Foreground="YellowGreen">Some Italic Text Here!</Italic> 
 5     </Paragraph> 
 6     <Paragraph> 
 7         <Underline Foreground="LightBlue">Some Underline Text Here!</Underline> 
 8     </Paragraph> 
 9     <Paragraph Foreground="Red"> 
10         <Bold >Some Bold Text Here!</Bold> 
11     </Paragraph> 
12 </RichTextBox>

运行结果:

源码:

MangoRichTextBoxDemo

转载于:https://www.cnblogs.com/qq278360339/archive/2012/06/11/2544810.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值