取出richTextBox裏面的內容
第一種方法:將richTextBox的內容以字符串的形式取出html
string xw = System.Windows.Markup.XamlWriter.Save(richTextBox.Document);
第二種方法:將richTextBox的類容以二進制數據的方法取出字體
FlowDocument document = richTextBox.Document; System.IO.Stream s = new System.IO.MemoryStream(); System.Windows.Markup.XamlWriter.Save(document, s); byte[] data = new byte[s.Length]; s.Position = 0; s.Read(data, 0, data.Length); s.Close();
賦值給richTextBox
第一種方法:將字符串轉換爲數據流賦值給richTextBox中 spa
System.IO.StringReader sr = new System.IO.StringReader(xw); System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr); richTextBox1.Document = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);
第二種方法:將二進制數據賦值給richTextBoxcode
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的方法orm
System.Windows.Documents.FlowDocument doc = richTextBox.Document;
doc.Blocks.Clear();
如何將一個String類型的字符串賦值給richTextBoxhtm
myRTB.Document = new FlowDocument(new Paragraph(new Run(myString))); FlowDocument doc = new FlowDocument(); Paragraph p = new Paragraph(); // Paragraph 相似於 html 的 P 標籤 Run r = new Run(myString); // Run 是一個 Inline 的標籤 p.Inlines.Add(r); doc.Blocks.Add(p); myRTB.Document = doc;
如何將richTextBox中的內容以rtf的格式徹底取出blog
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
複製 字符串
ToolBarCopy.Command = System.Windows.Input.ApplicationCommands.Copy;
剪切 string
toolBarCut.Command = System.Windows.Input.ApplicationCommands.Cut;
粘貼 it
ToolBarPaste.Command = System.Windows.Input.ApplicationCommands.Paste;
撤銷
ToolBarUndo.Command = System.Windows.Input.ApplicationCommands.Undo;
復原
ToolBarRedo.Command = System.Windows.Input.ApplicationCommands.Redo;
文字居中
toolBarContentCenter.Command = System.Windows.Documents.EditingCommands.AlignCenter;
文字居右
toolBarContentRight.Command = System.Windows.Documents.EditingCommands.AlignRight;
文字居左
toolBarContentLeft.Command = System.Windows.Documents.EditingCommands.AlignLeft;
有序排列
ToolBarNumbering.Command = System.Windows.Documents.EditingCommands.ToggleNumbering;
無序排列
ToolBarBullets.Command = System.Windows.Documents.EditingCommands.ToggleBullets;
字體變大
int fontSize = Convert.ToInt32(richTextBox.Selection.GetPropertyValue(TextElement.FontSizeProperty)); fontSize++; richTextBox.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, fontSize.ToString());