WPF中RichTextBox设置文本的样式

RichTextBox的内容操作方式,选中RichTextBox的内容方法:

  • 鼠标操作,用鼠标选中,然后用下面的方式来读取

    TextRange range=RichTextBox1.Selection  //获取选中项

    string text=range.Text;  //选中的文本内容

    range.Start //获取选中的开始位置

    range.End //获取选中的结束位置

  • 使用代码设置选中项

    new TextRange().Select(TextPointer startpoint, TextPointer endPoint);  //

通过代码来设置选中文字的样式代码:

  RichTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
  RichTextBox1.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, 12);

例:设置选中部分的前景色为红色

      _rtb.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));

     设置整个文本

     TextRange rag = new TextRange(_rtb.Document.ContentStart, _rtb.Document.ContentEnd);
     srag.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));

但有些时候,设置起不了作用,可右键该项目选择清理,然后重新运行即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值