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

原文地址:https://www.cnblogs.com/xiarifeixue/p/1905288.html