Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RichTextBox Class

Represents a rich editing control which operates on FlowDocument objects.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

[LocalizabilityAttribute(LocalizationCategory.Inherit)] 
[ContentPropertyAttribute("Document")] 
public class RichTextBox : TextBoxBase, IAddChild
/** @attribute LocalizabilityAttribute(LocalizationCategory.Inherit) */ 
/** @attribute ContentPropertyAttribute("Document") */ 
public class RichTextBox extends TextBoxBase implements IAddChild
LocalizabilityAttribute(LocalizationCategory.Inherit) 
ContentPropertyAttribute("Document") 
public class RichTextBox extends TextBoxBase implements IAddChild
<RichTextBox>
  Document
</RichTextBox>

FlowDocument is the only supported child element for a RichTextBox.

NoteImportant:

RichTextBox has built-in handling for the bubbling  MouseUp and MouseDown events. Consequently, custom event handlers that listen for MouseUp or MouseDown events from a RichTextBox will never be called. If you need to respond to these events, listen for the tunneling PreviewMouseUp and PreviewMouseDown events instead, or register the handlers with the HandledEventsToo argument (this latter option is only available through code). Do not mark the event handled unless you deliberately want to disable RichTextBox native handling of these events, and be aware that this has notable effects on the control's UI.

While RichTextBox supports copying and pasting of images, certain scenarios are currently unsupported and may not work properly. The following table summarizes these scenarios and expected results.

Image Copy Source

Image Paste Result

Image source is a graphic metafile

No image is pasted

Image source uses relative path or link

Garbled or blank image is pasted

Image source link does not end with an expected image format (.PNG, .JPG, .GIF)

Garbled or blank image is pasted

Image source copied from malformed RichText (RTF)

Link to image source is pasted (rather than image)

Text always wraps in a RichTextBox. If you do not want text to wrap then set the PageWidth on the FlowDocument to be larger than the width of the RichTextBox. However, once the page width is reached the text still wraps.

Horizontally and vertically aligning content within a RichTextBox is done with the HorizontalContentAlignment and VerticalContentAlignment properties. Aligning the RichTextBox within the layout of the page is done with the HorizontalAlignment and VerticalAlignment properties.

Scrollbars are not visible on a RichTextBox by default. To make a scrollbar visible, set the VerticalScrollBarVisibility property to Visible or Auto.

The following example declares a RichTextBox that contains a simple FlowDocument.

<RichTextBox Name="richTB">
  <FlowDocument>
    <Paragraph>
      <Run>Paragraph 1</Run>
    </Paragraph>
    <Paragraph>
      <Run>Paragraph 2</Run>
    </Paragraph>
    <Paragraph>
      <Run>Paragraph 3</Run>
    </Paragraph>
  </FlowDocument>
</RichTextBox>

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Documents;
namespace SDKSample
{
    public partial class RichTextBoxExample : Page
    {
        public RichTextBoxExample()
        {

            StackPanel myStackPanel = new StackPanel();

            // Create a FlowDocument to contain content for the RichTextBox.
            FlowDocument myFlowDoc = new FlowDocument();

            // Add paragraphs to the FlowDocument.
            myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
            myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
            myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
            RichTextBox myRichTextBox = new RichTextBox();

            // Add initial content to the RichTextBox.
            myRichTextBox.Document = myFlowDoc;
            
            myStackPanel.Children.Add(myRichTextBox);
            this.Content = myStackPanel;
            
        }
    }
}

More Code

How to: Extract the Text Content from a RichTextBox

This example shows how to extract the contents of a RichTextBox as plain text.

How to: Position a Custom Context Menu in a RichTextBox

This example shows how to position a custom context menu for a RichTextBox.

How to: Replace the Default Content Host for a RichTextBox

This example shows how to use Windows Presentation Foundation (WPF) styles to replace the default content host for a RichTextBox.

System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Media.Visual
         System.Windows.UIElement
           System.Windows.FrameworkElement
             System.Windows.Controls.Control
               System.Windows.Controls.Primitives.TextBoxBase
                System.Windows.Controls.RichTextBox

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

Show:
© 2014 Microsoft