Export (0) Print
Expand All
3 out of 8 rated this helpful - Rate this topic

RichTextBox Class

Updated: February 2009

Represents a rich editing control which operates on FlowDocument objects.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
[LocalizabilityAttribute(LocalizationCategory.Inherit)]
[ContentPropertyAttribute("Document")]
public class RichTextBox : TextBoxBase, 
	IAddChild
<RichTextBox>
  Document
</RichTextBox>

FlowDocument is the only supported child element for a RichTextBox.

Important noteImportant Note:

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)

Pasting HTML content into a RichTextBox might result in unexpected behavior because RichTextBox uses RTF format rather than directly using HTML format.

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.

Usually, the TextChanged event should be used to detect when the text in a TextBox or RichTextBox changes, rather then KeyDown as you might expect. For an example, see How to: Detect When Text in a TextBox Has Changed.

RichTextBox supports a variety of keyboard commands. For a list of keyboard commands, see EditingCommands.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.

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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Date

History

Reason

February 2009

Described how default styles change dependency properties.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.