The following is transferred from http://blog.csdn.net/yulongguiziyao/article/details/25330551.
1. Obtain the content that has been selected:
(1) using the RichTextBox.Document.Selection property
(2) Accessing the text in the "blocks" of the RichTextBox.Document.Blocks property
2. Add content to RichTextBox in XAML:
<richtextbox isspellcheckenabled= "True" >
<FlowDocument>
<Paragraph>
<!--here to add your content--
This is a richTextBox. I can <bold>bold</bold>, <italic>italicize</italic>, </Paragraph>
</FlowDocument>
</RichTextBox>
3. Shorten the segment spacing, similar to <br> instead of <P>
The method is to define the segment spacing using the style:
<RichTextBox>
<RichTextBox.Resources>
<style targettype= "{x:type Paragraph}" >
<setter property= "Margin" value= "0"/>
</Style>
</RichTextBox.Resources>
<FlowDocument>
<Paragraph>
This is my first paragraph ... see how there is ...
</Paragraph>
<Paragraph>
A no space anymore between it and the second paragraph?
</Paragraph>
</FlowDocument>
</RichTextBox>
4. After you have read the plain text file from the file, put it into the RichTextBox or put the text directly into the 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);
}
}
5. How to add text in RichTextBox
RichTextBox is a control in WPF that stores content that is rendered by its Document property. Document is a FlowDocument type.
FlowDocument are containers for placing block contents (Blocks) and inlines.
Block-level elements (blocks) include: paragraph,list,table,section
Inline elements include: Run,span,bold, Italic, Underline,hyperlink,linebreak,inlineuicontainer,floater, figure
RichTextBox Add text code:
String mytext= "hello!";
RichTextBox myrichtextbox=new RichTextBox ();
FlowDocument doc = new FlowDocument ();
Paragraph p = new Paragraph ();
Run R = new Run (myText);
P.inlines.add (R);//run-level elements added to the paragraph element's inline
Doc. Blocks.add (p);//paragraph-level elements added to a block-level element of a flow document
Myrichtextbox.document = doc;
}
6. Obtain the content of the specified RichTextBox:
private String GetText (RichTextBox RichTextBox)
{
TextRange TextRange = new TextRange (RichTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
return textrange.text;
}
7. Place the RTF (Rich Text Format) in the 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 (0, Seekorigin.begin);
Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
Textrange.load (Rtfmemorystream, dataformats.rtf);
}
}
}
8. Load the contents of the file as RichTextBox content
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) = = 0) {
DataFormat = Dataformats.xaml;
}
else if (String.Compare (ext, ". rtf", true) = = 0) {
DataFormat = dataformats.rtf;
}
Documenttextrange.load (stream, DataFormat);
}
}
9. Save the contents of the RichTextBox as a file:
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) = = 0) {
DataFormat = Dataformats.xaml;
}
else if (String.Compare (ext, ". rtf", true) = = 0) {
DataFormat = dataformats.rtf;
}
Documenttextrange.save (stream, DataFormat);
}
}
10. Make a simple editor:
<!--Window1.xaml--
<DockPanel>
<menu dockpanel.dock= "Top" >
<menuitem header= "_file" >
<menuitem header= "_open File" click= "Onopenfile"/>
<menuitem header= "_save" click= "Onsavefile"/>
<Separator/>
<menuitem header= "E_xit" click= "OnExit"/>
</MenuItem>
</Menu>
<richtextbox name= "RichTextBox1" ></RichTextBox>
</DockPanel>
Window1.xaml.cs
private void OnExit (object sender, EventArgs e) {
This. Close ();
}
private void Onopenfile (object sender, EventArgs e) {
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog ();
Ofd. Filter = "Text Files" (*.txt; *.xaml; *.rtf) |*.txt;*.xaml;*.rtf ";
Ofd. MultiSelect = false;
if (OFD. ShowDialog () = = True) {
LoadFile (OFD. Safefilename, RichTextBox1);
}
}
private void Onsavefile (object sender, EventArgs e) {
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog ();
SfD. Filter = "Text Files" (*.txt; *.xaml; *.rtf) |*.txt;*.xaml;*.rtf ";
if (SFD. ShowDialog () = = True) {
SaveFile (SFD). Safefilename, RichTextBox1);
}
}
Take out the contents of RichTextBox
The first method: Remove the contents of the RichTextBox as a string
String XW = System.Windows.Markup.XamlWriter.Save (richtextbox.document);
The second method: The RichTextBox class is extracted by means of binary data
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 ();
Assign a value to RichTextBox
The first method: Convert a string to a data stream assignment to RichTextBox
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);
The second method: assigning binary data to 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;
How to Empty RichTextBox
System.Windows.Documents.FlowDocument doc = richtextbox.document;
Doc. Blocks.clear ();
How to assign a string to RichTextBox
Myrtb.document = new FlowDocument (new Paragraph (New Run (myString));
FlowDocument doc = new FlowDocument ();
Paragraph p = new Paragraph (); Paragraph a P tag similar to HTML
Run R = new Run (myString); Run is an Inline label
P.inlines.add (R);
Doc. Blocks.add (P);
Myrtb.document = doc;
How to remove content from RichTextBox in full text format
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 ();
}
Operation RichTextBox
Copy Toolbarcopy.command = System.Windows.Input.ApplicationCommands.Copy;
Shear Toolbarcut.command = System.Windows.Input.ApplicationCommands.Cut;
Paste Toolbarpaste.command = System.Windows.Input.ApplicationCommands.Paste;
Revoke Toolbarundo.command = System.Windows.Input.ApplicationCommands.Undo;
Recovery Toolbarredo.command = System.Windows.Input.ApplicationCommands.Redo;
Text centered toolbarcontentcenter.command = System.Windows.Documents.EditingCommands.AlignCenter;
Text Right toolbarcontentright.command = System.Windows.Documents.EditingCommands.AlignRight;
Text left toolbarcontentleft.command = System.Windows.Documents.EditingCommands.AlignLeft;
Orderly arrangement Toolbarnumbering.command = System.Windows.Documents.EditingCommands.ToggleNumbering;
unordered permutation toolbarbullets.command = System.Windows.Documents.EditingCommands.ToggleBullets;
Font size becomes larger
int fontSize = Convert.ToInt32 (RichTextBox.Selection.GetPropertyValue (Textelement.fontsizeproperty));
fontsize++;
RichTextBox.Selection.ApplyPropertyValue (Textelement.fontsizeproperty, fontsize.tostring ());
WPF RichTextBox controls common methods and properties