Pregunta

How can I setup a PasswordBox in Silverlight 4 so that when it's empty it shows the text "Please enter password here" inside the PasswordBox?

I want the text to show up in the PasswordBox, while the cursor is blinking on the very beginning of the line. When the user starts typing, the text should disappear and the default password characters should be shown. When the user erases the password completely, the text should be shown again.

Is this possible with pure XAML?

If not, I'd also appreciate a solution with some event handlers and code behind. Thanks in advance!

¿Fue útil?

Solución 2

I now implemented the following solution. It's not pure XAML, but I think it's ok. In XAML I put a PasswordBox, followed by a TextBlock with the "Enter your password" text which overlaps the PasswordBox. In the event handler of the PasswordBox the TextBlock will be made invisible.

<PasswordBox x:Name="PasswordBox"
             PasswordChanged="PasswordBox_PasswordChanged" />
<TextBlock x:Name="TextBlockEnterPassword" FontStyle="Italic"
           Text="Enter your password here" IsHitTestVisible="False"/>

(Put the above together in one Grid cell.)

In the code behind, add the event handler:

private void PasswordBox_PasswordChanged( object sender, RoutedEventArgs e )
{
    // If the user enters something in the passwordbox, hide
    // the "Enter password" text; if the user erases the field,
    // show the text.
    if ( PasswordBox.Password.Length == 0 )
        TextBlockEnterPassword.Visibility = Visibility.Visible;
    else
        TextBlockEnterPassword.Visibility = Visibility.Collapsed;
}

Otros consejos

Try to switch between textbox and passwordbox, for example on GotFocus, LostFocus

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top