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));
但有些时候,设置起不了作用,可右键该项目选择清理,然后重新运行即可
鼠标操作,用鼠标选中,然后用下面的方式来读取
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));
但有些时候,设置起不了作用,可右键该项目选择清理,然后重新运行即可
相关文章推荐
- wpf datagrid 样式怎么设置默认选中行的颜色
- NSMutableAttributedString文本样式设置以及富文本展示
- CSS样式设置语法全解,样式优先级、值和单位、字体、文本、块级元素,行内元素,替换元素、非替换元素、display、float、position、table、li、光标、边距边框、轮廓、颜色背景
- WPF 设置DataGrid 鼠标移动改变行颜色样式
- WPF里给RichTextBox中的文本添加行号
- WPF操作RichTextBox 追加文本显示不同颜色
- iOS 计算富文本高度和设置段落样式
- 继续聊WPF——设置网格控件列标题的样式
- 05使用CSS设置字体和文本样式
- WPF下TextBox允许多行文本的设置
- TextView文本设置不同的样式
- android控件文本样式的设置方法
- CSS:设置文本与字体样式
- 沐风心扬C#编程速查系列之RichTextBox单行文本颜色设置
- 如何在XCode中设置文本样式
- css基础 设置所有h1标签的 文本颜色 对齐方式 边框样式 背景颜色
- 第六步:页面内的基本文本的样式(css)设置
- table里面怎么设置td的样式和Excel里面的表格一样&&当光标浮动td上到元素上时如何显示全部文本。
- WPF中设置TEXTBOX为多行文本输入框
- WPF中ComboBox控件显示的样式设置