1. 取得已被选中的内容:
(1)使用RichTextBox.Document.Selection属性
(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text

2.WPF RictTextBox内容清空方式:txtXml.Document.Blocks.Clear();

3. 从文件中读出纯文本文件后放进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);
}
} private void LoadText(RichTextBox richTextBox, string txtContent)
{
richTextBox.Document.Blocks.Clear();
Paragraph paragraph = new Paragraph();
paragraph.Text = txtContent;
richTextBox.Document.Blocks.Add(paragraph);
}

4. 取得指定RichTextBox的内容:

private string GetText(RichTextBox richTextBox)
{
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
return textRange.Text;
}

5. 将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(, SeekOrigin.Begin);
//Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
textRange.Load(rtfMemoryStream, DataFormats.Rtf);
}
}
}

6. 将文件中的内容加载为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) == )
{
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == )
{
dataFormat = DataFormats.Rtf;
}
documentTextRange.Load(stream, dataFormat);
}
}

7. 将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) == )
{
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == )
{
dataFormat = DataFormats.Rtf;
}
documentTextRange.Save(stream, dataFormat);
}
}

读取与写入图片和文本操作::::

读取RichTextBox的内容到string,将字符串保存到数据库的方法就不写了,大家都会

string GetTextByRichBox(RichTextBox box)
{
MemoryStream s = new MemoryStream();
TextRange documentTextRange = new TextRange(box.Document.ContentStart, box.Document.ContentEnd);
documentTextRange.Save(s, DataFormats.XamlPackage);
return Convert.ToBase64String(s.ToArray());
}

将string的内容转换成图片显示在RichTextBox中

private ShowTextToRichBox(RichTextBox box)
{
MemoryStream s = new MemoryStream((Convert.FromBase64String(Convert.ToString(dr[“D_DESC”]))));
TextRange TR = new TextRange(box.Document.ContentStart, box.Document.ContentEnd);
TR.Load(s, DataFormats.XamlPackage);
}

WPF 中RichTextBox控件用法细讲的更多相关文章

  1. WPF中Ribbon控件的使用

    这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...

  2. WPF中查找控件的扩展类

    在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...

  3. WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书

    原文:WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书 最近项目中使用弹出控件Popup,发现弹出框的对齐方式在不同的系统中存在不同(Popup在win10上是 ...

  4. WPF中Image控件的Source属性

    原文:WPF中Image控件的Source属性 imgBook 是一个Image控件,在后台代码中我想给它指定Source的属性.我先如下方式进行: Uri uri = new Uri(strImag ...

  5. WPF中PasswordBox控件的Password属性的数据绑定

    原文:WPF中PasswordBox控件的Password属性的数据绑定 英文原文:http://www.wpftutorial.net/PasswordBox.html 中文原文:http://bl ...

  6. 浅谈WPF中对控件的位图特效(WPF Bitmap Effects)

    原文:浅谈WPF中对控件的位图特效(WPF Bitmap Effects) -------------------------------------------------------------- ...

  7. 示例:WPF中Slider控件封装的缓冲播放进度条控件

    原文:示例:WPF中Slider控件封装的缓冲播放进度条控件 一.目的:模仿播放器播放进度条,支持缓冲任务功能 二.进度: 实现类似播放器中带缓存的播放样式(播放区域.缓冲区域.全部区域等样式) 实现 ...

  8. WPF中TreeView控件SelectedItemChanged方法的MVVM绑定

    问题描述:左侧treeview控件中点击不同类别的节点时,右侧的页面会显示不同的权限.比如对于My Publications,拥有Modify和Delete两种权限,对于My Subscription ...

  9. WPF中TreeView控件数据绑定和后台动态添加数据(二)

    写在前面:在(一)中,介绍了TreeView控件MVVM模式下数据绑定的方法.在这篇文章中,将总结给节点添加事件的方法,这样说有些不对,总之实现的效果就是点击某个节点,将出现对应于该节点的页面或者数据 ...

随机推荐

  1. 【hdu 3863】No Gambling

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65568/32768 K (Java/Others) Total Submission(s) ...

  2. nil和Nil和NULL的判断

    ,nil和Nil和NULL的判断 开 发过程中,我们通过http请求,后台返回json数据,而有时数据里某一字段的值为null-,然后我们把此值赋值给 NSArray,NSdictionary,或是N ...

  3. 【codeforces 779A】Pupils Redistribution

    [题目链接]:http://codeforces.com/contest/779/problem/A [题意] 让你把两个组的5个人的数目都变成一样的. 支持交换操作; 问你最少需要交换几次. [题解 ...

  4. boost::any在降低模块之间耦合性的应用

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在开发大型系统中,遵循这样一个原则:模块之间低耦合,模块内高内聚.比如系统中模块有界面模块和算法模块两种,一般是界面模 ...

  5. TensorFlow 学习(六) —— TensorFlow 与 numpy 的交互

    1. 将 numpy 下的多维数组(ndarray)转化为 tensor a = np.zeros((3, 3)) ta = tf.convert_to_tensor(a) with tf.Sessi ...

  6. Android应用《撕开美女衣服》的实现过程及源代码

    现在很多Android市场中都能找到关于美女的应用,比如 撕开美女衣服.吹裙子等. 这些应用的下载量挺大的,作为Android的开发人员或者一名技术人员我们不能只局限在欣赏应用的层面,很多时候需要我们 ...

  7. mac在终端打开应用程序

    今天研究了下mac终端的启动流程.以下以sublime为例,介绍怎么在mac的终端中加入app启动方法. 方法1 :使用"open -a /Applications/Sublime\ Tex ...

  8. error: expected declaration or statement at end of input----solved

    error: expected declaration or statement at end of input 解决方法: 1.程序缺少一个括号相应地 2.而不添加头文件 版权声明:本文博主原创文章 ...

  9. 数据访问层之Repository

    数据访问层之Repository   接上文 项目架构开发:数据访问层之Logger 本章我们继续IRepository开发,这个仓储与领域模式里边的仓储有区别,更像一个工具类,也就是有些园友说的“伪 ...

  10. 解决android模拟器无法上网问题

    1.  将 android的tool增加到,windows 环境变量 path中, D:\Android\android-sdk_r3-windows\android-sdk-windows\tool ...