More on "Avalon" RichTextBox

As promised, I learned more so I'm sharing. Last time I figured out how to stuff plain text into the RichTextBox, but that's not very "rich", is it? 

How do I put RTF (rich text format) into a RichTextBox?
It turns out the support for this seems to be through the TextRange class. TextRange offers a "Load" method which takes a string representing a data format (which you can pick from the DataFormats class) and a stream. If you currently have a string of RTF formatting, you can stuff the string in a MemoryStream, then pass it to the TextRange. Here's a handy helper function for it:

        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);

            //Create a MemoryStream of the Rtf content

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);
}
}
}

How do I load a file with "rich" content in it?
Here again, you can use the TextRange class to load, but you pass in a FileStream instead of a MemoryStream. 

   private static void LoadFile(string filename, RichTextBox richTextBox) {
if (string.IsNullOrEmpty(filename)) {
throw new ArgumentNullException();
}
if (!File.Exists(filename)) {
throw new FileNotFoundException();
}

            // open the file for reading
using (FileStream stream = File.OpenRead(filename)) {
// create a TextRange around the entire document
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

// sniff out what data format you've got
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);
}
}

How do I Save a file with "rich" content in it?
TextRange also has a Save! Here's the code

    private static void SaveFile(string filename, RichTextBox richTextBox) {
if (string.IsNullOrEmpty(filename)) {
throw new ArgumentNullException();
}
          

            // open the file for reading
using (FileStream stream = File.OpenWrite(filename)) {
// create a TextRange around the entire document
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

                // sniff out what data format you've got
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);
}
}

Putting it all together - a very simple editor

Replacing the contents of the Window with this DockPanel, adding the SaveFile, LoadFile helper methods and the event handlers for open and save, we can get a very simple rich text 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);
}
}