Share via


RichTextBox.IsReadOnly Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets a value that determines whether the user can change the text in the RichTextBox.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property IsReadOnly As Boolean
public bool IsReadOnly { get; set; }
<RichTextBox IsReadOnly="bool"/>

Property Value

Type: System.Boolean
true if the RichTextBox is read-only; otherwise, false. The default is false.

Remarks

If the RichTextBox is read-only, no user input or edits are allowed, but programmatic changes can be made. The user can still select text and the selection highlight still appears. The KeyUp and KeyDown events are marked as handled. UI elements and hyperlinks in a RichTextBox are active only in read-only mode. For example, they can respond to input and receive focus only when they are in read-only mode. The cursor does not display in the RichTextBox when the IsReadOnly property is set to true.

NoteNote:

Silverlight for Windows Phone In Silverlight for Windows Phone, RichTextBox is read-only. Do not use this property if you are using RichTextBox in a Windows Phone application.

Examples

The following shows how to set the IsReadOnly property in XAML and code.

<RichTextBox IsReadOnly="True">
    <Paragraph>
        <Hyperlink NavigateUri="http://www.msdn.com" TargetName="_blank">Hyperlinks</Hyperlink> are enabled in a read-only RichTextBox.
    </Paragraph>
</RichTextBox>
Private Sub ReadOnlyRTB()
    'Create a new RichTextBox.
    Dim MyRTB As RichTextBox = New RichTextBox
    ' Create a Run of plain text and hyperlink.
    Dim myRun As Run = New Run
    myRun.Text = " are enabled in a read-only RichTextBox."
    Dim MyLink As Hyperlink = New Hyperlink
    MyLink.Inlines.Add("Hyperlinks")
    MyLink.NavigateUri = New Uri("http://www.msdn.com")
    MyLink.TargetName = "_blank"
    ' Create a paragraph and add the Run and hyperlink to it.
    Dim myParagraph As Paragraph = New Paragraph
    myParagraph.Inlines.Add(MyLink)
    myParagraph.Inlines.Add(myRun)
    ' Add the paragraph to the RichTextBox.
    MyRTB.Blocks.Add(myParagraph)
    'Add the RichTextBox to the StackPanel.
    MySP.Children.Add(MyRTB)
End Sub
private void ReadOnlyRTB()
{
    //Create a new RichTextBox.
    RichTextBox MyRTB = new RichTextBox();

    // Create a Run of plain text and hyperlink.
    Run myRun = new Run();
    myRun.Text = " are enabled in a read-only RichTextBox.";
    Hyperlink MyLink = new Hyperlink();
    MyLink.Inlines.Add("Hyperlinks");
    MyLink.NavigateUri = new Uri("http://www.msdn.com");
    MyLink.TargetName = "_blank";

    // Create a paragraph and add the Run and hyperlink to it.
    Paragraph myParagraph = new Paragraph();
    myParagraph.Inlines.Add(MyLink);
    myParagraph.Inlines.Add(myRun);

    // Add the paragraph to the RichTextBox.
    MyRTB.Blocks.Add(myParagraph);

    //Add the RichTextBox to the StackPanel.
    MySP.Children.Add(MyRTB);
}

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.