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模式下数据绑定的方法.在这篇文章中,将总结给节点添加事件的方法,这样说有些不对,总之实现的效果就是点击某个节点,将出现对应于该节点的页面或者数据 ...
随机推荐
- SpringMVC中支持多视图解析
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/suo082407128/article/details/70173301 在SpringMVC模式当 ...
- [Django] Creating an app, models and database
To add a new app, first cd to the project. Then run: python manage.py startapp scrumboard After that ...
- Wpf的布局舍入属性(可以解决软件字体模糊的问题)
原文:Wpf的布局舍入属性(可以解决软件字体模糊的问题) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/HK_JY/article/details/ ...
- 【t007】棋盘放置指南车问题
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 按照国际象棋的规则,车可以攻击与之处在同一行或同一列上的棋子.指南车是有方向的车.横向指南车可以攻击与之 ...
- Eclipse 一直不停 building workspace... 完美解决总结
Eclipse 一直不停 building workspace... 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭 3.maven下载lib挂起 等..二.解决总结 (1).解决方法 ...
- OpenCV中CvSVM部分函数解读
CvSVM::predict函数解析:无论是Mat接口还是CvMat接口终于都是通过指针的形式调用的.也就是终于都是调用的下面函数实现的 float CvSVM::predict( const flo ...
- MapReduce自定义InputFormat,RecordReader
MapReduce默认的InputFormat是TextInputFormat,且key是偏移量,value是文本,自定义InputFormat需要实现FileInputFormat,并重写creat ...
- linux 时间同步ntp
配置前准备:关闭防火墙,配置好hosts,ssh免密登录 1.选定同步的标准,我是以hadoop002(设置为当前时间)作为同步标准,hadoop003(时间是2018年3月21,使用date -s进 ...
- IP packet transmission using vehicular transport
In one embodiment, a first stationary router may detect a disconnected backhaul link to a destinatio ...
- numpy 辨异(三)—— hstack/column_stack,linalg.eig/linalg.eigh
1. np.hstack np.column_stack >>> np.hstack([np.array([1, 2, 3]), np.array([4, 5, 6])]) arra ...