TextBox.MaxLength 属性

定义

获取或设置可在文本框中手动输入的最大字符数。Gets or sets the maximum number of characters that can be manually entered into the text box.

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; }
member this.MaxLength : int with get, set
Public Property MaxLength As Integer

属性值

可在文本框中手动输入的最大字符数。The maximum number of characters that can be manually entered into the text box. 默认值为 0,指示没有限制。The default is 0, which indicates no limit.

属性

示例

下面的示例演示如何TextBox MaxLength使用500个字符创建一个。The following example shows how to create a TextBox with a MaxLength of 500 characters.

<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

注解

您可以使用此属性来限制控件中输入的文本长度, 以获取邮政编码和电话号码之类的值。You can use this property to restrict the length of text entered in the control for values such as postal codes and telephone numbers. 当数据存储在数据库中时, 还可以使用此属性来限制输入的文本长度, 以便在控件中输入的文本不会超过数据库中对应字段的最大长度。You can also use this property to restrict the length of text entered when the data is to be stored in a database so that the text entered into the control does not exceed the maximum length of the corresponding field in the database.

此属性不会影响以编程方式添加的字符。This property does not affect characters that are added programmatically.

当此属性设置为0时, 可在控件中输入的文本的最大长度仅受可用内存的限制。When this property is set to 0, the maximum length of the text that can be entered in the control is limited only by available memory.

依赖项属性信息Dependency Property Information

标识符字段Identifier field MaxLengthProperty
元数据属性设置为trueMetadata properties set to true NoneNone

适用于

另请参阅