TextBox.MaxLength 属性

定义

获取或设置可在文本框中手动输入的最大字符数。

public:
 property int MaxLength { int get(); void set(int value); };
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Modifiability=System.Windows.Modifiability.Unmodifiable)]
public int MaxLength { get; set; }
[<System.Windows.Localizability(System.Windows.LocalizationCategory.None, Modifiability=System.Windows.Modifiability.Unmodifiable)>]
member this.MaxLength : int with get, set
Public Property MaxLength As Integer

属性值

Int32

可在文本框中手动输入的最大字符数。 默认值为 0,指示没有限制。

属性

示例

以下示例演示如何创建 TextBox 包含 MaxLength 500 个字符的字符。

<Page  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel>
    <TextBox 
      Text="Initial text in TextBox" 
      Width="200" 
      TextAlignment="Center"
      TextWrapping="Wrap" 
      MaxLength="500"
      MinLines="1" 
      MaxLines="5" />
  </StackPanel>
</Page>
StackPanel myStackPanel = new StackPanel();

//Create TextBox
TextBox myTextBox = new TextBox();
myTextBox.Width = 200;

// Put some initial text in the TextBox.
myTextBox.Text = "Initial text in TextBox";

// Set the maximum characters a user can manually type
// into the TextBox.
myTextBox.MaxLength = 500;
myTextBox.MinLines = 1;

// Set the maximum number of lines the TextBox will expand to 
// accomidate text. Note: This does not constrain the amount of 
// text that can be typed. To do that, use the MaxLength property.
myTextBox.MaxLines = 5;

// The text typed into the box is aligned in the center.
myTextBox.TextAlignment = TextAlignment.Center;

// When the text reaches the edge of the box, go to the next line.
myTextBox.TextWrapping = TextWrapping.Wrap;

myStackPanel.Children.Add(myTextBox);
this.Content = myStackPanel;
Dim myStackPanel As New StackPanel()

'Create TextBox
Dim myTextBox As New TextBox()
myTextBox.Width = 200

' Put some initial text in the TextBox.
myTextBox.Text = "Initial text in TextBox"

' Set the maximum characters a user can manually type
' into the TextBox.
myTextBox.MaxLength = 500
myTextBox.MinLines = 1

' Set the maximum number of lines the TextBox will expand to 
' accomidate text. Note: This does not constrain the amount of 
' text that can be typed. To do that, use the MaxLength property.
myTextBox.MaxLines = 5

' The text typed into the box is aligned in the center.
myTextBox.TextAlignment = TextAlignment.Center

' When the text reaches the edge of the box, go to the next line.
myTextBox.TextWrapping = TextWrapping.Wrap

myStackPanel.Children.Add(myTextBox)
Me.Content = myStackPanel

注解

可以使用此属性限制控件中输入的值(如邮政编码和电话号码)的文本长度。 还可以使用此属性来限制在数据库中存储数据时输入的文本长度,以便输入到控件中的文本不超过数据库中相应字段的最大长度。

此属性不会影响以编程方式添加的字符。

当此属性设置为 0 时,控件中可以输入的文本的最大长度仅受可用内存的限制。

依赖项属性信息

标识符字段 MaxLengthProperty
元数据属性设置为 true

适用于

另请参阅