共用方式為


RichTextBox.TextAlignment Property

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

Gets or sets how the text should be aligned in the RichTextBox.

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

Syntax

'Declaration
Public Property TextAlignment As TextAlignment
public TextAlignment TextAlignment { get; set; }
<RichTextBox TextAlignment="textAlignmentValue"/>

XAML Values

  • textAlignmentValue
    A named constant of the TextAlignment enumeration, such as Left.

Property Value

Type: System.Windows.TextAlignment
One of the TextAlignment enumeration values. The default is Left.

Remarks

This property returns the current alignment. Setting this property adjusts the contents of the RichTextBox to reflect the specified alignment.

Examples

The following example shows how you can right-align the entire contents of the RichTextBox.

Run this sample

<!--Create a RichTextBox and a Button.-->
<StackPanel>
    <RichTextBox x:Name="MyRTB" Height="100" Width="400" />
    <Button x:Name="MyButton1" Content="Right-Align" Height="30" Width="100" Click="button_Click" />
</StackPanel>
'Right-align the content in RichTextBox on clicking the button.
Private Sub button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim MyBC As BlockCollection = InlineAssignHelper(MyBC, MyRTB.Blocks)
    For Each b As Block In MyBC
        b.TextAlignment = TextAlignment.Right
    Next
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function
//Right-align the content in RichTextBox on clicking the button.
private void button_Click(object sender, RoutedEventArgs e)
{
   BlockCollection MyBC = MyRTB.Blocks;
   foreach (Block b in MyBC)
       b.TextAlignment = TextAlignment.Right;               
}

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.