Partager via


PasswordBox.MaxLength Property

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

Gets or sets the maximum length for passwords to be handled by this PasswordBox.

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

Syntax

'Declaration
Public Property MaxLength As Integer
public int MaxLength { get; set; }
<PasswordBox MaxLength="int"/>

Property Value

Type: System.Int32
An integer specifying the maximum length, in character, for passwords to be handled by this PasswordBox.
A value of zero (0) means no limit.
The default value is 0 (no length limit).

Exceptions

Exception Condition
ArgumentOutOfRangeException

The property is set to a negative value.

Remarks

Dependency property identifier field: MaxLengthProperty

This property only restricts password entered manually by the user; it has no effect on programmatic manipulation of the Password property.

Examples

The following snippet sets the MaxLength property to 8. This example is part of a larger example available in the PasswordBox class overview.

<TextBlock Text="Type password here" Margin="20,10,0,0"/>
<PasswordBox x:Name="MyPWBox1" Margin="20,10,0,0" PasswordChanged="MyPWBox1_PasswordChanged" MaxLength="8" Height="35" Width="200" HorizontalAlignment="Left" />
<TextBlock Text="Password" Margin="20,10,0,0"/>
<TextBox x:Name="ReadOnlyTB" Margin="20,10,0,0" HorizontalAlignment="Left" IsReadOnly="True" Height="35" Width="200" />

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.