TextAlignment Property

TextBox.TextAlignment Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

'Declaration
Public Property TextAlignment As TextAlignment
<TextBox TextAlignment="textAlignmentValue"/>

XAML Values

textAlignmentValue

A named constant of the TextAlignment enumeration, such as Center.

Property Value

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

Dependency property identifier field: TextAlignmentProperty.

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

The following code snippets show how you can use the TextAlignment property in XAML and code.


<StackPanel>
    <TextBox Margin="20,20,0,0" Text="A text box that demonstrates TextWrapping, TextAlignment, MaxLength, and AcceptsReturn" 
             Width="300" Height="150" TextWrapping="Wrap" TextAlignment="Center" MaxLength="500" AcceptsReturn="True" />
    <TextBox Margin="20,20,0,0" Text="A text box that demonstrates HorizontalScrollBarVisibility and VerticalScrollBarVisibility" 
             Width="300" Height="150" AcceptsReturn="True" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" />
</StackPanel>



Public Sub New()
    MyBase.New()
    InitializeComponent()
    CreateControls()
End Sub

Private Sub CreateControls()

    'Create stack panel
    Dim MyStackPanel As StackPanel = New StackPanel

    'Create the first text box
    Dim MyTB1 As TextBox = New TextBox
    MyTB1.Width = 300
    MyTB1.Height = 150
    MyTB1.Text = "A text box that demonstrates TextWrapping, TextAlignment, MaxLength and AcceptsReturn"
    MyTB1.TextWrapping = TextWrapping.Wrap
    MyTB1.TextAlignment = TextAlignment.Center
    MyTB1.MaxLength = 500
    MyTB1.AcceptsReturn = True
    MyTB1.Margin = New Thickness(20, 20, 0, 0)

    'Create the second text box
    Dim MyTB2 As TextBox = New TextBox
    MyTB2.Margin = New Thickness(20, 20, 0, 0)
    MyTB2.Text = "A text box that demonstrates HorizontalScrollBarVisibility and VerticalScrollBarVisibility"
    MyTB2.Width = 300
    MyTB2.Height = 150
    MyTB2.AcceptsReturn = True
    MyTB2.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible
    MyTB2.VerticalScrollBarVisibility = ScrollBarVisibility.Visible

    'Add the text boxes to the stack panel
    MyStackPanel.Children.Add(MyTB1)
    MyStackPanel.Children.Add(MyTB2)
    Me.Content = MyStackPanel
End Sub


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft