Silverlight Tip of the Day #47 – How to Implement a Password Box
With the release of Silverlight 2 RC0 there are three new controls we will be discussing that were not available for beta 2. The three new controls with a Tip of the Day for each include:
For this tip we will be exploring the PasswordBox control. Password boxes are great in that they hide the characters a user is typing for privacy and security reasons. It is essential to use this control whenever you are receiving a password from a user.
The following code below shows how to declare, size and position a password box in your XAML:
<PasswordBox Canvas.Top="20" x:Name="UserPassword" Width="200"></PasswordBox>
By default Silverlight uses a “dot” as the character to hide the characters the user typed.
You can override this character using the PasswordChar property:
<PasswordBox PasswordChar="*" Canvas.Top="20" x:Name="UserPassword" Width="200"></PasswordBox>
You can also do all this programmatically in your code behind:
PasswordBox passBox = new PasswordBox();
passBox.Width = 200;
passBox.SetValue(Canvas.TopProperty, (double)300);
passBox.PasswordChar = '*';
MyCanvas.Children.Add(passBox);
Note that we have to take the extra step to add it to our Canvas control so that it appears in the element tree.
Thank you,
--Mike Snow
Subscribe in a reader