Share via


RichTextBox.Blocks Property

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

Gets the contents of the RichTextBox.

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

Syntax

'Declaration
Public ReadOnly Property Blocks As BlockCollection
public BlockCollection Blocks { get; }
<RichTextBox ...>
blocksContent
</RichTextBox>

XAML Values

  • blocksContent
    Zero or more object elements for types that derive from Block. Typically, these are Paragraph elements.

Property Value

Type: System.Windows.Documents.BlockCollection
A BlockCollection that contains the contents of the RichTextBox.

Remarks

The Blocks property is the content property of RichTextBox. It is a collection of Paragraph elements. Content in each Paragraph element can contain the following elements:

A InlineUIContainer can contain a UIElement, such as an Image or a Button.

A Span is a collection of Inline elements.

Examples

The following example shows how you can set content in a RichTextBox using XAML and code.

<!--A RichTextBox with intial content in it.-->
<RichTextBox VerticalScrollBarVisibility="Auto">
    <Paragraph>
        A RichTextBox with <Bold>initial content</Bold> in it.
    </Paragraph>
</RichTextBox>
'A RichTextBox with intial content in it.
Private Sub ContentRTB()
    'Create a new RichTextBox with its VerticalScrollBarVisibility property set to Auto.
    Dim MyRTB As New RichTextBox()
    MyRTB.VerticalScrollBarVisibility = ScrollBarVisibility.Auto

    ' Create a Run of plain text and some bold text.
    Dim myRun1 As New Run()
    myRun1.Text = "A RichTextBox with "
    Dim myBold As New Bold()
    myBold.Inlines.Add("initial content ")
    Dim myRun2 As New Run()
    myRun2.Text = "in it."

    ' Create a paragraph and add the Run and Bold to it.
    Dim myParagraph As New Paragraph()
    myParagraph.Inlines.Add(myRun1)
    myParagraph.Inlines.Add(myBold)
    myParagraph.Inlines.Add(myRun2)

    ' Add the paragraph to the RichTextBox.
    MyRTB.Blocks.Add(myParagraph)

    'Add the RichTextBox to the StackPanel.
    MySP.Children.Add(MyRTB)
End Sub
//A RichTextBox with intial content in it.
private void ContentRTB()
{
    //Create a new RichTextBox with its VerticalScrollBarVisibility property set to Auto.
    RichTextBox MyRTB = new RichTextBox();
    MyRTB.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

    // Create a Run of plain text and some bold text.
    Run myRun1 = new Run();
    myRun1.Text = "A RichTextBox with ";
    Bold myBold = new Bold();
    myBold.Inlines.Add("initial content ");
    Run myRun2 = new Run();
    myRun2.Text = "in it.";

    // Create a paragraph and add the Run and Bold to it.
    Paragraph myParagraph = new Paragraph();
    myParagraph.Inlines.Add(myRun1);
    myParagraph.Inlines.Add(myBold);
    myParagraph.Inlines.Add(myRun2);

    // 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.