WPF RichTextBox读取存储文本的方法和常用属性
1. 取得已被选中的内容:
(1)使用 RichTextBox.Document.Selection属性
(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text
2. 在XAML中增加内容给RichTextBox:
<RichTextBox IsSpellCheckEnabled="True">
<FlowDocument>
<Paragraph>
<!-- 这里加上你的内容 -->
This is a richTextBox. I can <Bold>Bold</Bold>, <Italic>Italicize</Italic>, <Hyperlink>Hyperlink stuff</Hyperlink> right in my document.
</Paragraph>
</FlowDocument>
</RichTextBox>
3. 缩短段间距,类似<BR>,而不是<P>
方法是使用Style定义段间距:
<RichTextBox>
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
<FlowDocument>
<Paragraph>
This is my first paragraph... see how there is...
</Paragraph>
<Paragraph>
a no space anymore between it and the second paragraph?
</Paragraph>
</FlowDocument>
</RichTextBox>
4. 从文件中读出纯文本文件后放进RichTextBox或直接将文本放进RichTextBox中:
private void LoadTextFile(RichTextBox richTextBox, string filename)
{
richTextBox.Document.Blocks.Clear();
using (StreamReader streamReader = File.OpenText(filename)) {
Paragraph paragraph = new Paragraph();
paragraph.Text = streamReader.ReadToEnd();
richTextBox.Document.Blocks.Add(paragraph);
}
}
5.如何在RichTextBox中添加文本
RichTextBox 是WPF中的一个控件,它存储的内容由其 Document 属性来呈现。Document 是一个 FlowDocument 类型。
FlowDocument 是放置块内容(Blocks)和Inlines的容器 。
块级元素(Block)包括:Paragraph,List,Table,Section
Inline元素包括:Run,Span,Bold、Italic、Underline,Hyperlink,LineBreak,InlineUIContainer,Floater、Figure
richtextbox添加文本代码:
string myText="hello!";
RichTextBox MyRichTextBox=new RichTextBox ();
FlowDocument doc = new FlowDocument();
Paragraph p = new Paragraph();
Run r = new Run(myText);
p.Inlines.Add(r);//Run级元素添加到Paragraph元素的Inline
doc.Blocks.Add(p);//Paragraph级元素添加到流文档的块级元素
MyRichTextBox.Document = doc;
}
6. 取得指定RichTextBox的内容:
private string GetText(RichTextBox richTextBox)
{
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
return textRange.Text;
}
7. 将RTF (rich text format)放到RichTextBox中:
private static void LoadRTF(string rtf, RichTextBox richTextBox)
{
if (string.IsNullOrEmpty(rtf)) {
throw new ArgumentNullException();
}
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream rtfMemoryStream = new MemoryStream()) {
using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream)) {
rtfStreamWriter.Write(rtf);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
//Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
textRange.Load(rtfMemoryStream, DataFormats.Rtf);
}
}
}
8. 将文件中的内容加载为RichTextBox的内容
private static void LoadFile(string filename, RichTextBox richTextBox)
{
if (string.IsNullOrEmpty(filename)) {
throw new ArgumentNullException();
}
if (!File.Exists(filename)) {
throw new FileNotFoundException();
}
using (FileStream stream = File.OpenRead(filename)) {
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
string dataFormat = DataFormats.Text;
string ext = System.IO.Path.GetExtension(filename);
if (String.Compare(ext, ".xaml",true) == 0) {
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == 0) {
dataFormat = DataFormats.Rtf;
}
documentTextRange.Load(stream, dataFormat);
}
}
9. 将RichTextBox的内容保存为文件:
private static void SaveFile(string filename, RichTextBox richTextBox)
{
if (string.IsNullOrEmpty(filename)) {
throw new ArgumentNullException();
}
using (FileStream stream = File.OpenWrite(filename)) {
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
string dataFormat = DataFormats.Text;
string ext = System.IO.Path.GetExtension(filename);
if (String.Compare(ext, ".xaml", true) == 0) {
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == 0) {
dataFormat = DataFormats.Rtf;
}
documentTextRange.Save(stream, dataFormat);
}
}
10. 做个简单的编辑器:
<!-- Window1.xaml -->
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Open File" Click="OnOpenFile"/>
<MenuItem Header="_Save" Click="OnSaveFile"/>
<Separator/>
<MenuItem Header="E_xit" Click="OnExit"/>
</MenuItem>
</Menu>
<RichTextBox Name="richTextBox1"></RichTextBox>
</DockPanel>
// Window1.xaml.cs
private void OnExit(object sender, EventArgs e) {
this.Close();
}
private void OnOpenFile(object sender, EventArgs e) {
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true) {
LoadFile(ofd.SafeFileName, richTextBox1);
}
}
private void OnSaveFile(object sender, EventArgs e) {
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
if (sfd.ShowDialog() == true) {
SaveFile(sfd.SafeFileName, richTextBox1);
}
}
WPF RichTextBox读取存储文本的方法和常用属性的更多相关文章
- ABAP读取长文本的方法
SAP中所有的项目文本都存在以下两张数据表中: 1. STXH 抬头项目文本 透明表 2. STXL 明细项目文本 透明表 长文本读取方法 首先在STXH和STXL中根据OBJECT NAME ...
- MATLAB读取写入文本数据最佳方法 | Best Method for Loading & Saving Text Data Using MATLAB
MATLAB读取文件有很多方法.然而笔者在过去进行数据处理中,由于函数太多,相互混杂,与C#,Python等语言相比,反而认为读取文本数据比较麻烦.C#和Python等高级语言中,对于大部分的文本数据 ...
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...
- 架设传奇时打开DBC数据库出错或读取DBC失败解决方法
架设传奇时打开DBC数据库出错或读取DBC失败解决方法 DBC右键-属性-高级-管理员身份运行 即可
- 计算纯文本情况下RichTextBox实际高度的正确方法(.NET)
2016-07-17重大更新 其实有更好.更系统的方法,也是最近才发现的,分享给大家!! /// <summary> /// /// </summary> ...
- C# richtextbox 自动下拉到最后 方法 & RichTextBox读取txt中文后出现乱码
C# richtextbox 自动滚动到最后 光标到最后 自动显示最后一行 private void richTextBox1_TextChanged(object sender, EventArg ...
- WPF RichTextBox 控件常用方法和属性
以下内容转自 http://blog.csdn.net/yulongguiziyao/article/details/25330551. 1. 取得已被选中的内容: (1)使用 RichTextBox ...
- WPF RichTextBox 当前光标后一个字符是文档的第几个字符
WPF RichTextBox 当前光标后一个字符是文档的第几个字符 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-05 参考: TextP ...
- Javascript写入txt和读取txt文件的方法
文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Va ...
随机推荐
- c++操作io常见命令
用c++练习下 系统常见io命令. 1)显示文档的文本 2)统计文本单词数字 3)列出目录所有文件 ,递归思路 4)查找第一个匹配的字符. 5)文本单词排序, 快速排序,其实还是递归思路 6)文本单词 ...
- 老笔记本_Win7_U盘_ReadyBoost
老笔记本 Win7 U盘 ReadyBoost 值得尝试
- mysql 权限控制
1.mysql的权限是,从某处来的用户对某对象的权限. 2.mysql的权限采用白名单策略,指定用户能做什么,没有指定的都不能做. 3.权限校验分成两个步骤: a.能不能连接,检查从哪里来,用户名和密 ...
- apt-get的常用用法
我们装完linux后的第一件事情就是安装软件了,下面的命令可以帮助你在Ubuntu发行版或基于Debain的发行版上快速的安装软件: sudo apt-get install package-name ...
- 【夯实Mysql基础】记一次mysql语句的优化过程!
1. [事件起因] 今天在做项目的时候,发现提供给客户端的接口时间很慢,达到了2秒多,我第一时间,抓了接口,看了运行的sql,发现就是 2个sql慢,分别占了1秒多. 一个sql是 链接了5个表同 ...
- Android网络编程系列 一 Socket抽象层
在<Android网络编程>系列文章中,前面已经将Java的通信底层大致的描述了,在我们了解了TCP/IP通信族架构及其原理,接下来我们就开始来了解基于tcp/ip协议层的Socket抽 ...
- OpenGL的glViewport视口变换函数详解[转]
调用glViewPort函数来决定视见区域,告诉OpenGL应把渲染之后的图形绘制在窗体的哪个部位.当视见区域是整个窗体时,OpenGL将把渲染结果绘制到整个窗口. void glViewPort(G ...
- [转载]java中try 与catch的使用
留着以后看 原文地址:与catch的使用">java中try 与catch的使用作者:碌碌如玉 try{ //代码区 }catch(Exception e){ //异常处理 } 代码区 ...
- JSON对象如何转化为字符串?
序列化 定义 指将 JavaScript 值转化为 JSON 字符串的过程. JSON.stringify() 能够将 JavaScript 值转换成 JSON 字符串.JSON.stringify( ...
- Openvpn 安装简述
1.固定本地IP地址 vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.同步时间.安装ntpd yum install ntpddate netupda ...