WPF-20:richtextbox相关操作(转)
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相关操作(转)的更多相关文章
- WPF RichTextBox相关总结
由于公司涉及到聊天对话框的功能,就想到了RichTextBox,查阅相关资料,总结下: 一.RichTextBox的内容相关的类 1.1RichTextBox的内容结构 RichTexBox是个可编辑 ...
- 从零自学Hadoop(20):HBase数据模型相关操作上
阅读目录 序 介绍 命名空间 表 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...
- 6月20日 Django中ORM介绍和字段、字段参数、相关操作
一.Django中ORM介绍和字段及字段参数 二.Django ORM 常用字段和参数 三.Django ORM执行原生SQL.在Python脚本中调用Django环境.Django终端打印SQL语句 ...
- python操作mysql数据库的相关操作实例
python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称 ...
- php对二维数组进行相关操作(排序、转换、去空白等)
php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04 这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...
- linux下进程相关操作
一.定义和理解 狭义定义:进程是正在运行的程序的实例. 广义定义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动. 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的 ...
- DataTable相关操作,筛选,取前N条数据,获取指定列数据
DataTable相关操作,筛选,取前N条数据,获取指定列数据2013-03-12 14:50 by Miracle520, 2667 阅读, 0 评论, 收藏, 编辑 1 #region DataT ...
- JqGrid相关操作
JqGrid相关操作 根据jqGrid的文档,要想生成一个jqGrid,最直接的方法就是:$("#grid_id").jqGrid(options);也就是通过选择符得到一个tab ...
- cmd 下登陆ftp及相关操作
cmd 下登陆ftp及相关操作 2011-08-09 20:34:28| 分类: 小技巧|字号 订阅 一.举例 假设FTP地址为“ 61.129.83.39”(大家试验的时候不要以这个FTP去试,应 ...
随机推荐
- 用Delphi实现Windows的鼠标钩子函数
Delphi是基于PASCAL语言的Windows编程工具,功能十分强大.然而在Delphi的帮助文件中,对Windows API函数的说明沿袭了 VC 的格式,和VC一样,对很多API函数的用法没有 ...
- 基于visual Studio2013解决面试题之0507字符串转整数
题目
- LINUX 命令行编辑
向 <-前 后 -> 删除 ctrl + d 删除光标所在位置上的字符相当于VIM里x或者dl ctrl + h 删除光标 ...
- Swift - 使用atlas图集实现动画效果(SpriteKit游戏开发)
我们通常继承SKSpriteNode来实现游戏中的元素,除了可以使用图片作为纹理皮肤外.我们还可以使用动画纹理集来实现动画播放. 动画纹理集的制作也很简单,首先要有一套动画序列图,然后把它们放到一个文 ...
- SqlServer中的数据类型UniqueIdentifier
SqlServer中的数据类型UniqueIdentifier究竟是什么东东? 该类型一般用来做为主键使用,可用SQL语法的newid()来生成一个唯一的值.我想请问的是,这个值是一个长整型的数据值呢 ...
- Eclipse Workspace Unavailable
开启Eclipse 提示: Workspace Unavailable: Workspace in use or cannot be created, choose a different one. ...
- 记录路径dp-4713-Permutation
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4713 题目大意: 题意同HDU 3092这不过这题要输出路径. 解题思路: 思路同HDU 3092. ...
- PowerShell与Unix Shell对比:八大实例
PowerShell与Unix Shell对比:八大实例 本文将从八个实例对比PowerShell和Unix Shell,通常是Linux Bourne Shell(包括sh.ksh和bash等).二 ...
- fake it till you become it
fake it till you become it_你泛起山川烟波里的不是我._百度空间 fake it till you become it
- sqlserver 存储过程学习笔记(二) 在项目中的应用<多表分页>
(1)存储过程建立 USE [NewPlat] GO /****** Object: StoredProcedure [dbo].[usp_PagingLarge] Script Date: ...