WPF 中RichTextBox控件用法细讲
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控件用法细讲的更多相关文章
- WPF中Ribbon控件的使用
这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...
- WPF中查找控件的扩展类
在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...
- WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书
原文:WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书 最近项目中使用弹出控件Popup,发现弹出框的对齐方式在不同的系统中存在不同(Popup在win10上是 ...
- WPF中Image控件的Source属性
原文:WPF中Image控件的Source属性 imgBook 是一个Image控件,在后台代码中我想给它指定Source的属性.我先如下方式进行: Uri uri = new Uri(strImag ...
- WPF中PasswordBox控件的Password属性的数据绑定
原文:WPF中PasswordBox控件的Password属性的数据绑定 英文原文:http://www.wpftutorial.net/PasswordBox.html 中文原文:http://bl ...
- 浅谈WPF中对控件的位图特效(WPF Bitmap Effects)
原文:浅谈WPF中对控件的位图特效(WPF Bitmap Effects) -------------------------------------------------------------- ...
- 示例:WPF中Slider控件封装的缓冲播放进度条控件
原文:示例:WPF中Slider控件封装的缓冲播放进度条控件 一.目的:模仿播放器播放进度条,支持缓冲任务功能 二.进度: 实现类似播放器中带缓存的播放样式(播放区域.缓冲区域.全部区域等样式) 实现 ...
- WPF中TreeView控件SelectedItemChanged方法的MVVM绑定
问题描述:左侧treeview控件中点击不同类别的节点时,右侧的页面会显示不同的权限.比如对于My Publications,拥有Modify和Delete两种权限,对于My Subscription ...
- WPF中TreeView控件数据绑定和后台动态添加数据(二)
写在前面:在(一)中,介绍了TreeView控件MVVM模式下数据绑定的方法.在这篇文章中,将总结给节点添加事件的方法,这样说有些不对,总之实现的效果就是点击某个节点,将出现对应于该节点的页面或者数据 ...
随机推荐
- 动态获取server时间
近期在做的这个项目要获取到后台的数据.然后使用html5绘制曲线.曾经都是在自己电脑上使用没有发现有什么不正常的地方,昨天连接同事的server发现有段数据怎么都对不上了,開始我还以为程序有问题呢,找 ...
- 文件控制 fcntl函数具体解释
摘要:本文主要讨论文件控制fcntl函数的基本应用.dup函数能够拷贝文件描写叙述符,而fcntl函数与dup函数有着异曲同工之妙.而且还有更加强大的功能,能够获取或设置已打开文件的性质,操作文件锁. ...
- MySQL分区表使用方法
原文:MySQL分区表使用方法 1. 确认MySQL服务器是否支持分区表 命令: show plugins; 2. MySQL分区表的特点 在逻辑上为一个表,在物理上存储在多个文件中 HASH分区(H ...
- cordova插件整理
原文:cordova插件整理 1.获取当前应用的版本号 cordova plugin add cordova-plugin-app-version 2.获取网络连接信息 cordova plugin ...
- Android菜鸟的成长笔记(18)——绑定本地Service并与之通信
在上一篇中介绍了Service与Activity的区别及Service两种启动方式中的第一种启动方式startService(). 我们会发现用startService().stopService() ...
- Function函数
一般大家都用这个写法来定义一个函数: function Name([parameters]){ functionBody }; //alert(typeof Name) // Function 当我们 ...
- css 单选框 样式 填充自定义背景 after
input[type='radio'] //width 16px //height 16px display none //input[type='radio']:chcked // backgoun ...
- JS简单验证password强度
<input type="password" id="password" value=""/><button id=&qu ...
- 自学 Python
如何系统地自学 Python? 最近开始系统的学习Python,以及整理的一些资料.github记录着个人自学 Python 的过程,持续更新.欢迎大家一起来完善这个自学Python学习的项目,给 ...
- wpf中的倒影效果实现
原文:wpf中的倒影效果实现 <TextBox Name="txt" FontSize="30" ...