开发者

Silverlight PasswordBox showing "Please enter password here" inside

开发者 https://www.devze.com 2023-03-28 22:56 出处:网络
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?

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 a开发者_Python百科ppreciate a solution with some event handlers and code behind. Thanks in advance!


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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消