您的位置:首页 > Web前端 > CSS

WPF中RichTextBox设置文本的样式

2011-06-20 10:01 645 查看
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));

但有些时候,设置起不了作用,可右键该项目选择清理,然后重新运行即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: