title: Injecting XAML into a RichEditBox
body: <p>I'm currently trying to port Matthew Manela's <a href="http://code.msdn.microsoft.com/windowsdesktop/Converting-between-RTF-and-aaa02a6e" rel="nofollow">"Converting between RTF and XAML"</a> code sample to WinRT</p>
  
  <p>I've got the HTML to XAML code working, but I've hit a snag when getting it into a <code>RichEditBox</code>. </p>
  
  <p>Matthew's code is WPF based, and uses the following function to convert XAML to RTF.</p>
  
  <pre><code>private static string ConvertXamlToRtf(string xamlText) 
  { 
      var richTextBox = new RichTextBox(); 
      if (string.IsNullOrEmpty(xamlText)) return ""; 
      var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); 
      using (var xamlMemoryStream = new MemoryStream()) 
      { 
          using (var xamlStreamWriter = new StreamWriter(xamlMemoryStream)) 
          { 
              xamlStreamWriter.Write(xamlText); 
              xamlStreamWriter.Flush(); 
              xamlMemoryStream.Seek(0, SeekOrigin.Begin); 
              textRange.Load(xamlMemoryStream, DataFormats.Xaml); 
          } 
      } 
      using (var rtfMemoryStream = new MemoryStream()) 
      { 
          textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); 
          textRange.Save(rtfMemoryStream, DataFormats.Rtf); 
          rtfMemoryStream.Seek(0, SeekOrigin.Begin); 
          using (var rtfStreamReader = new StreamReader(rtfMemoryStream)) 
          { 
              return rtfStreamReader.ReadToEnd(); 
          } 
      } 
  }
  </code></pre>
  
  <p>I've tried rewriting this in WinRT using <code>RichEditBox</code>, but come up against some issues. Most noteably, WPF <code>TextRange</code> accepts a XAML dataformat, but WinRT <code>ITextRange</code> doesn't have this. However, I know that I can inject XAML directly into a <code>RichTextBlock</code> control. </p>
  
  <p>Is there any way to copy the text from a <code>RichTextBlock</code> and paste it into a RichEditBox, programmatically? </p>
  
  <p><strong>OR</strong>, failing that, is there a way to convert HTML to RTF that works in WinRT / Windows Store Apps? </p>