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));
但有些时候,设置起不了作用,可右键该项目选择清理,然后重新运行即可