Using XAML RichTextBox in WPF

XAML RichTextBox in WPF

A XAML RichTextBox represents a rich text box. This tutorial shows you how to use a RichTextBox control available in WPF.

Introduction

The RichTextBox control allows you to view and edit text, paragraph, images, tables and other rich text format contents.

The RichTextBox tag represents a RichTextBox control in XAML.

  1. <RichTextBox></RichTextBox>  
The Width and Height properties represent the width and the height of a RichTextBox. The Name property represents the name of the control, that is a unique identifier of a control. The Margin property tells the location of a RichTextBox on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.

The following code snippet sets the name, height and width of a RichTextBox control. The code also sets the horizontal alignment to left and the vertical alignment to top.
  1. <RichTextBox Margin="10,10,0,13" Name="RichTextBox1" HorizontalAlignment="Left"   
  2.                  VerticalAlignment="Top" Width="500" Height="300" />  
Displaying and Edit Text

A RichTextBox control hosts a collection of RichTextBoxItem. The following code snippet adds items to a RichTextBox control.
  1. <RichTextBox Margin="10,10,0,13" Name="RichTextBox1" HorizontalAlignment="Left"   
  2.              VerticalAlignment="Top" Width="500" Height="300">  
  3.     <FlowDocument>  
  4.         <Paragraph>  
  5.             I am a flow document. Would you like to edit me?  
  6.             <Bold>Go ahead.</Bold>                  
  7.         </Paragraph>  
  8.         
  9.         <Paragraph Foreground="Blue">            
  10.             I am blue I am blue I am blue.    
  11.         </Paragraph>  
  12.     </FlowDocument>          
  13. </RichTextBox>  
The preceding code generates Figure 1 where you can begin editing text right away.

RichTextBox with editable text

Figure 1. RichTextBox with editable text

Creating and Using RichTectBox Dynamically

In the previous section, we saw how to create and use a RichTextBox in XAML. WPF provides the RichTextBox class that represents a RichTextBox control. In this section, we will see how to use this class to create and use a RichTextBox control dynamically. 

The code listed in Listing 1 creates a FlowDocument, adds a paragraph to the flow document and sets the Document property of the RichTextBox to FlowDocument.

  1. private void CreateAndLoadRichTextBox()  
  2. {  
  3.     // Create a FlowDocument  
  4.     FlowDocument mcFlowDoc = new FlowDocument();  
  5.   
  6.     // Create a paragraph with text  
  7.     Paragraph para = new Paragraph();  
  8.     para.Inlines.Add(new Run("I am a flow document. Would you like to edit me? "));  
  9.     para.Inlines.Add(new Bold(new Run("Go ahead.")));  
  10.   
  11.     // Add the paragraph to blocks of paragraph  
  12.     mcFlowDoc.Blocks.Add(para);  
  13.   
  14.     // Create RichTextBox, set its hegith and width  
  15.     RichTextBox mcRTB = new RichTextBox();  
  16.     mcRTB.Width = 560;  
  17.     mcRTB.Height = 280;  
  18.   
  19.     // Set contents  
  20.     mcRTB.Document = mcFlowDoc;  
  21.   
  22.     // Add RichTextbox to the container  
  23.     ContainerPanel.Children.Add(mcRTB);       
  24. }  
Listing 1.

The output of Listing 1 generates Figure 2.

Listing 1 doc

Figure 2

Enable Spelling Check

A RichTextBox control comes with spell check functionality out-of-the-box. By setting the SpellCheck.IsEnabled property to true enables spell checking in a RichTextBox.
  1. SpellCheck.IsEnabled="True"  
You can set this in code as follows:
  1. mcRTB.SpellCheck.IsEnabled = true;  
Now if you type some text, the wrong word would be underlined with the Red color. See in Figure 3.

RichTextBox with Spell Check Enabled

Figure 3. RichTextBox with Spell Check Enabled

Loading a Document in RichTextBox

We can use the RichTextBox.Items.Remove or RichTextBox.Items.RemoveAt methods to delete an item from the collection of items in the RichTextBox. The RemoveAt method takes the index of the item in the collection.

Now, we modify our application and add a new button called Delete Item. The XAML code for this button looks as in the following:
  1. private void OpenMenuItem_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     OpenFileDialog dlg = new OpenFileDialog();  
  4.     dlg.InitialDirectory = "c:\\";  
  5.     dlg.Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*";  
  6.     dlg.RestoreDirectory = true;  
  7.     if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
  8.     {  
  9.   
  10.         LoadTextDocument(dlg.FileName);  
  11.     }  
  12. }  
  13.   
  14.   
  15. private void LoadTextDocument(string fileName)  
  16. {  
  17.     TextRange range;  
  18.     System.IO.FileStream fStream;  
  19.     if (System.IO.File.Exists(fileName))  
  20.     {  
  21.         range = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd);  
  22.         fStream = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate);  
  23.         range.Load(fStream, System.Windows.DataFormats.Text );  
  24.         fStream.Close();  
  25.     }  
  26. }   
Summary

In this article, I discussed how to create and use a RichTextBox control available in WPF. We saw how to load text programmatically or load a text file.

Up Next
    Ebook Download
    View all
    Learn
    View all