0

如何PasswordBox在 Silverlight 4 中设置 a,以便当它为空时,它会在 PasswordBox内显示文本“请在此处输入密码” ?

我希望文本显示在 PasswordBox 中,而光标在该行的最开头闪烁。当用户开始输入时,文本应该消失并且应该显示默认密码字符。当用户完全删除密码时,文本应再次显示。

纯 XAML 可以做到这一点吗?

如果没有,我也很欣赏带有一些事件处理程序和代码的解决方案。提前致谢!

4

2 回答 2

0

我现在实施了以下解决方案。它不是纯 XAML,但我认为没关系。在 XAML 中,我放了一个PasswordBox,后跟一个TextBlock与 PasswordBox 重叠的“输入您的密码”文本。在 PasswordBox 的事件处理程序中,TextBlock 将变为不可见。

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

(将以上内容放在一个Grid单元格中。)

在后面的代码中,添加事件处理程序:

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;
}
于 2011-11-08T12:23:49.260 回答
0

尝试在文本框和密码框之间切换,例如在 GotFocus、LostFocus 上

于 2011-11-07T11:07:15.237 回答