Represents a rich text control that supports formatted text, hyperlinks, inline images, and other rich content.
<TemplateVisualStateAttribute(Name := "ReadOnly", GroupName := "CommonStates")> _ <TemplateVisualStateAttribute(Name := "Disabled", GroupName := "CommonStates")> _ <TemplateVisualStateAttribute(Name := "Unfocused", GroupName := "FocusStates")> _ <TemplateVisualStateAttribute(Name := "Normal", GroupName := "CommonStates")> _ <TemplateVisualStateAttribute(Name := "Focused", GroupName := "FocusStates")> _ <TemplateVisualStateAttribute(Name := "Valid", GroupName := "ValidationStates")> _ <ContentPropertyAttribute("Blocks", True)> _ <TemplatePartAttribute(Name := "ContentElement", Type := GetType(FrameworkElement))> _ <TemplateVisualStateAttribute(Name := "InvalidUnfocused", GroupName := "ValidationStates")> _ <TemplateVisualStateAttribute(Name := "InvalidFocused", GroupName := "ValidationStates")> _ <TemplateVisualStateAttribute(Name := "MouseOver", GroupName := "CommonStates")> _ Public Class RichTextBox _ Inherits Control
[TemplateVisualStateAttribute(Name = "ReadOnly", GroupName = "CommonStates")] [TemplateVisualStateAttribute(Name = "Disabled", GroupName = "CommonStates")] [TemplateVisualStateAttribute(Name = "Unfocused", GroupName = "FocusStates")] [TemplateVisualStateAttribute(Name = "Normal", GroupName = "CommonStates")] [TemplateVisualStateAttribute(Name = "Focused", GroupName = "FocusStates")] [TemplateVisualStateAttribute(Name = "Valid", GroupName = "ValidationStates")] [ContentPropertyAttribute("Blocks", true)] [TemplatePartAttribute(Name = "ContentElement", Type = typeof(FrameworkElement))] [TemplateVisualStateAttribute(Name = "InvalidUnfocused", GroupName = "ValidationStates")] [TemplateVisualStateAttribute(Name = "InvalidFocused", GroupName = "ValidationStates")] [TemplateVisualStateAttribute(Name = "MouseOver", GroupName = "CommonStates")] public class RichTextBox : Control
<RichTextBox .../> -or- <RichTextBox ...> blocksContent </RichTextBox>
One or more object elements for types that derive from Block. Typically, these are Paragraph elements.
The RichTextBox type exposes the following members.
You can use the RichTextBox control to display, enter, format, and edit rich text.
In this release of Silverlight for Windows Phone, RichTextBox is read-only.
The following illustration shows an application that uses the RichTextBox.
RichTextBox provides more advanced formatting features than the TextBox and TextBlock controls. You can apply character and paragraph formatting to the text in the RichTextBox. For example, you can apply Bold, Italic, and Underline to any portion of the text in the control.
The content property of RichTextBox is the Blocks property, which is based on the Paragraph element. Content can contain many types of elements including formatted text, hyperlinks, and images.
You can use the Hyperlink element to display hyperlinks in a RichTextBox. You must set the IsReadOnly property of the RichTextBox to true for hyperlinks to work.
You can use the RichTextBox to display text with UI elements that are derived from UIElement, such as images. You can do this by using the InlineUIContainer.
RichTextBox has a read-only mode. You can enable read-only mode by setting the IsReadOnly property to true.
To apply the same property settings to multiple RichTextBox controls, use the Style property. To change the visual structure and visual behavior of a RichTextBox, copy and modify its default style and template. For more information, see Control Customization.
Dependency properties for this control might be set by the default style of the control. If a dependency property for a RichTextBox is set by its default style, the property might change from its default value when the RichTextBox appears in the application. For more information, see Dependency Property Value Precedence. You can get the default style and template for RichTextBox from RichTextBox Styles and Templates.
Setting a visual property will only have an effect if that property is both present in RichTextBox control's default template and is set by using a TemplateBinding. You can find a list of visual properties in the "Changing the Visual Structure of a Control" section in Customizing the Appearance of an Existing Control by Using a ControlTemplate.
The following table provides information about tasks that are typically associated with the RichTextBox.
Task
Implementation
Set content
Use the Paragraph element to set content. For more information, see RichTextBox Overview.
Display text with inline UIElement objects such as an Image or a Calendar.
Use the InlineUIContainer element. For more information, see RichTextBox Overview.
Display hyperlinks
Use the Hyperlink element. For more information, see RichTextBox Overview.
Display content in read-only mode
Use the IsReadOnly property. For more information, see RichTextBox Overview.
Programmatically select content
Use the Selection property and the TextSelection class. For more information, see RichTextBox Overview.
Programmatically format content
Use the GetPropertyValue and ApplyPropertyValue methods. For more information, see RichTextBox Overview.
Programmatically traverse content
Use the TextPointer class.
Silverlight for Windows Phone
The following code shows how to create a RichTextBox with a paragraph and some bold text.
<!--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); }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.