New to Telerik UI for WPF? Download free 30-day trial

Using XamlFormatProvider

XamlFormatProvider makes it easy to import and export RadDocument to/from XAML format. This is the native format of RadRichTextBox and Using it guarantees that anything in the document will be preserved exactly the same as it is in the control.

Since R2 2023 SP1 the XamlFormatProvider automatically verifies the imported XAML. More information is available in the Xaml Verification article.

To use XamlFormatProvider, you should add a reference to:

  • Telerik.Windows.Documents.FormatProviders.Xaml.dll

Import

In order to import a XAML document, you can use the overloads of the Import() method.

Example 1 shows how to use XamlFormatProvider to import XAML document from a file.

Example 1: Import XAML document from a file

XamlFormatProvider xamlformatProvider = new XamlFormatProvider(); 
using (FileStream inputStream = new FileStream("XamlDocument.xaml",FileMode.Open)) 
{ 
    this.radRichTextBox.Document = xamlformatProvider.Import(inputStream); 
} 

Export

In order to export a document to XAML, you need to use the Export() method of XamlFormatProvider.

Example 2 shows how to use XamlFormatProvider to export RadDocument to a file.

Example 3: Export document to a file

XamlFormatProvider provider = new XamlFormatProvider(); 
using (FileStream output =  new FileStream("XamlDocument.xaml",FileMode.OpenOrCreate)) 
{ 
    RadDocument document = this.radRichTextBox.Document; 
    provider.Export(document, output); 
} 

However, the format providers cannot be used in XAML and you have to implement a logic that will call their Import() and Export() methods. This is something that you might not want to be concerned with when using RadRichTextBox in a data bound scenarios. For such cases, the DataProvider classes are used. They wrap the FormatProviders' functionality and allow its usage in XAML.

See Also

In this article