WPF中的RichTextBox

 

原文链接:http://blog.csdn.net/wuzhengqing1/article/details/7010902

 

取出richTextBox里面的内容


第一种方法:将richTextBox的内容以字符串的形式取出

string str = System.Windows.Markup.XamlWriter.Save(richTextBox.Document);

 

 

第二种方法:将richTextBox的类容以二进制数据的方法取出

复制代码
     FlowDocument     doc    = richTextBox.Document;
     System.IO.Stream stream = new System.IO.MemoryStream(); 
     System.Windows.Markup.XamlWriter.Save(doc, stream);         
     byte[] data = new byte[stream.Length];
     stream.Position = 0;
     stream.Read(data, 0, data.Length);
     stream.Close();
复制代码

 

 

赋值给richTextBox

第一种方法:将字符串转换为数据流赋值给richTextBox中  

  System.IO.StringReader sr = new System.IO.StringReader(str);
  System.Xml.XmlReader   xr = System.Xml.XmlReader.Create(sr);
  richTextBox1.Document     = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);

 


第二种方法:将二进制数据赋值给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    para = new Paragraph();//------Paragraph 类似于 html 的 P 标签
Run          run  = new Run(myString);//----Run 是一个 Inline 的标签
para.Inlines.Add(run);
doc.Blocks.Add(para);
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(); }
复制代码

 

RichTextBox的操作:

复制代码
       using System.Windows.Input;
       using System.Windows.Documents;
       
//---复制,剪切,粘贴,撤销,重做 ToolBarCopy.Command
= ApplicationCommands.Copy; ToolBarCut.Command = ApplicationCommands.Cut; ToolBarPaste.Command = ApplicationCommands.Paste; ToolBarUndo.Command = ApplicationCommands.Undo; ToolBarRedo.Command = ApplicationCommands.Redo; //---文字居中/右/左 ToolBarContentCenter.Command = EditingCommands.AlignCenter; ToolBarContentRight.Command = EditingCommands.AlignRight; ToolBarContentLeft.Command = EditingCommands.AlignLeft; //---有序/无序排列 ToolBarNumbering.Command = EditingCommands.ToggleNumbering; ToolBarBullets.Command = EditingCommands.ToggleBullets;//---字体变大 int fontSize = Convert.ToInt32(richTextBox.Selection.GetPropertyValue(
TextElement.FontSizeProperty)); fontSize
++; richTextBox.Selection.ApplyPropertyValue(TextElement.FontSizeProperty,
fontSize.ToString());
复制代码

 

posted @   (•̀ω•́)y  阅读(350)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示