WPF中RichTextBox设置文本的样式

来源:互联网 发布:数据库触发器简单事例 编辑:程序博客网 时间:2024/04/29 12:29

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));

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

原创粉丝点击