共用方式為


TextBox.TextWrapping Property

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

Gets or sets how line breaking occurs if a line of text extends beyond the available width of the text box.

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

Syntax

'Declaration
Public Property TextWrapping As TextWrapping
public TextWrapping TextWrapping { get; set; }
<TextBlock TextWrapping="Wrap"/>

Property Value

Type: System.Windows.TextWrapping
One of the TextWrapping values. The default is NoWrap.

Remarks

Dependency property identifier field: TextWrappingProperty.

You should consider the height and width of the text box when you set the TextWrapping property. Any text that extends beyond the bounds of the text box will not be displayed.

Examples

The following code snippets show how you can use the TextWrapping 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="50" 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="50" 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 = 50
    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 = 50
    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
public Page()
{
    InitializeComponent();
    CreateControls();
}

private void CreateControls()
{
    //Create stack panel
    StackPanel MyStackPanel = new StackPanel();

    //Create the first text box
    TextBox MyTB1 = new TextBox();
    MyTB1.Width = 300;
    MyTB1.Height = 50;
    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
    TextBox MyTB2 = 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 = 50;
    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);
    this.Content = MyStackPanel;
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

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