WPF中的richtextbox与winform中的richtextbox的使用不同,看看下面的基本操作:

一、取出richTextBox里面的内容

 (1)将richTextBox的内容以字符串的形式取出

 string xw = System.Windows.Markup.XamlWriter.Save(richTextBox.Document);

(2)将richTextBox的类容以二进制数据的方法取出

     FlowDocument document = richTextBox.Document;
System.IO.Stream s = new System.IO.MemoryStream();
System.Windows.Markup.XamlWriter.Save(document, s);
byte[] data = new byte[s.Length];
s.Position = 0;
s.Read(data, 0, data.Length);
s.Close();

二、richTextBox赋值
 (1)将字符串转换为数据流赋值给richTextBox中

 System.IO.StringReader sr = new System.IO.StringReader(xw);
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr);
richTextBox1.Document = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);

(2)将二进制数据赋值给richTextBox

    System.IO.Stream ss = new System.IO.MemoryStream(data);
FlowDocument doc = System.Windows.Markup.XamlReader.Load(ss) as FlowDocument;
ss.Close();
richTextBox1.Document = doc;

三、清空RichTextBox的方法

System.Windows.Documents.FlowDocument doc = RichTextBox.Document;
doc.Blocks.Clear();

四、如何将一个String类型的字符串赋值给richTextBox

myRTB.Document = new FlowDocument(new Paragraph(new Run(myString)));
FlowDocument doc = new FlowDocument();
Paragraph p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
Run r = new Run(myString); // Run 是一个 Inline 的标签
p.Inlines.Add(r);
doc.Blocks.Add(p);
myRTB.Document = doc;

五、将richTextBox中的内容以rtf的格式完全取出

     string rtf = string.Empty;
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
textRange.Save(ms, System.Windows.DataFormats.Rtf);
ms.Seek(0, System.IO.SeekOrigin.Begin);
System.IO.StreamReader sr = new System.IO.StreamReader(ms);
rtf = sr.ReadToEnd();
}

六、其他操作

 复制:   ToolBarCopy.Command = System.Windows.Input.ApplicationCommands.Copy;
剪切: toolBarCut.Command = System.Windows.Input.ApplicationCommands.Cut;
粘贴: ToolBarPaste.Command = System.Windows.Input.ApplicationCommands.Paste;
撤销: ToolBarUndo.Command = System.Windows.Input.ApplicationCommands.Undo;
复原: ToolBarRedo.Command = System.Windows.Input.ApplicationCommands.Redo;
文字居中: toolBarContentCenter.Command = System.Windows.Documents.EditingCommands.AlignCenter;
文字居右: toolBarContentRight.Command = System.Windows.Documents.EditingCommands.AlignRight;
文字居左: toolBarContentLeft.Command = System.Windows.Documents.EditingCommands.AlignLeft;
有序排列: ToolBarNumbering.Command = System.Windows.Documents.EditingCommands.ToggleNumbering;
无序排列: ToolBarBullets.Command = System.Windows.Documents.EditingCommands.ToggleBullets;
字体变大:
int fontSize = Convert.ToInt32(richTextBox.Selection.GetPropertyValue(TextElement.FontSizeProperty));
fontSize++;
richTextBox.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, fontSize.ToString());

WPF-20:richtextbox相关操作(转)的更多相关文章

  1. WPF RichTextBox相关总结

    由于公司涉及到聊天对话框的功能,就想到了RichTextBox,查阅相关资料,总结下: 一.RichTextBox的内容相关的类 1.1RichTextBox的内容结构 RichTexBox是个可编辑 ...

  2. 从零自学Hadoop(20):HBase数据模型相关操作上

    阅读目录 序 介绍 命名空间 表 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...

  3. 6月20日 Django中ORM介绍和字段、字段参数、相关操作

    一.Django中ORM介绍和字段及字段参数 二.Django ORM 常用字段和参数 三.Django ORM执行原生SQL.在Python脚本中调用Django环境.Django终端打印SQL语句 ...

  4. python操作mysql数据库的相关操作实例

    python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称 ...

  5. php对二维数组进行相关操作(排序、转换、去空白等)

    php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04   这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...

  6. linux下进程相关操作

    一.定义和理解 狭义定义:进程是正在运行的程序的实例. 广义定义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动. 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的 ...

  7. DataTable相关操作,筛选,取前N条数据,获取指定列数据

    DataTable相关操作,筛选,取前N条数据,获取指定列数据2013-03-12 14:50 by Miracle520, 2667 阅读, 0 评论, 收藏, 编辑 1 #region DataT ...

  8. JqGrid相关操作

    JqGrid相关操作 根据jqGrid的文档,要想生成一个jqGrid,最直接的方法就是:$("#grid_id").jqGrid(options);也就是通过选择符得到一个tab ...

  9. cmd 下登陆ftp及相关操作

    cmd 下登陆ftp及相关操作 2011-08-09 20:34:28|  分类: 小技巧|字号 订阅 一.举例 假设FTP地址为“ 61.129.83.39”(大家试验的时候不要以这个FTP去试,应 ...

随机推荐

  1. Linux下SSH Session复制

    羡慕Windows下secureCRT的Session Copy功能,一直在寻找Linux下类似的软件,殊不知SSH本身就支持此功能. 特别感谢阿干同学的邮件分享. 详细方法 ? 1 2 3 4 Li ...

  2. win2k/xp查看当前进程

    win2k/xp查看当前进程 tasklist tasklist | find "关键字" 可以对结果进行过滤 关闭当前某个进程 taskkill /pid 程序的PID号码 wi ...

  3. 1.1.3-学习Opencv与MFC混合编程之---画图工具 通过对话框进行工具的参数设置 画曲线 绘图校正

    源代码:http://download.csdn.net/detail/nuptboyzhb/3961688 l 对话框 1.“插入”->“资源”->“对话框” 2.对话框属性如下: 双击 ...

  4. android launcher开发之图标背景以及默认配置

    1:然后我自己看了一下桌面图标的载入过程: 桌面第一次载入时是默认读取一个xml配置文件,完毕配置工作.这个配置文件在Launcher文件夹下, 路径是:\Launcher\res\xml\defau ...

  5. php等号(==)与全等(===)

    <?php require_once 'Person.php'; header("content-type:text/html;charset=utf-8"); $perso ...

  6. May Day Holiday

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practic ...

  7. 树莓派学习笔记——使用文件IO操作GPIO SysFs方式

    0 前言     本文描写叙述假设通过文件IO sysfs方式控制树莓派 GPIO端口.通过sysfs方式控制GPIO,先訪问/sys/class/gpio文件夹,向export文件写入GPIO编号, ...

  8. 求刷Kindle Fire HD的方法

    前几天入手了台Amazon Kindle Fire HD 其系统是经过Amazon尝试改造过的Android,用起来很不爽,想刷个CM10之类的,求教程和工具.

  9. struts 2吊牌s:if 、s:iterator注意

    疏忽,也没有相应的总结.实际上JSTL标签Struts2标签混淆.导致一些上述问题的细节.今天我给从下一个总结,同 后不要再犯这种错误. 总喜欢在s:if标签里面使用$,导致各种数据读不出来. str ...

  10. Android 手势&amp;触摸事件 MotionEvent

    1.http://blog.csdn.net/omg_2012/article/details/7881443 这篇相当好啊 2.http://blog.csdn.net/android_tutor/ ...