WP8 Toolkit PhoneTextBox的密码效果?

时间:2014-05-19 18:36:27

标签: c# windows-phone-8 textbox passwords wpftoolkit

我想在我的WP8项目中添加密码文本框。

显而易见的方法是PasswordBox;但是,我想要包含一个看似不可能的占位符。

所以我在WP8工具包中使用了PhoneTextBox功能,它允许占位符("提示")。但是,该功能似乎没有办法将字段指定为密码字段。

我想要的只是小圆点而不是字符:)

2 个答案:

答案 0 :(得分:1)

这就是我所做的。使用passwordBox作为小圆点的密码掩蔽效果,并使用普通文本框以浅灰色的颜色显示提示。

Xaml在这里:

        <!--used a textbox to show watermark and the other is a password box-->
        <TextBox x:Name="PasswordWatermark" TextWrapping="Wrap" Text="{Binding LocalizedResources.Password, Source={StaticResource LocalizedStrings}}" Foreground="{StaticResource PhoneTextBoxReadOnlyBrush}" IsHitTestVisible="False"/>
        <PasswordBox Name="PwdBox" LostFocus="PasswordBox_LostFocus" Opacity="0" GotFocus="PasswordBox_GotFocus" Password="{Binding Password, Mode=TwoWay}" BorderThickness="0" FontFamily="Segoe UI"  KeyDown="PwdBox_KeyDown" PasswordChanged="PwdBox_PasswordChanged"/>

只需确保设置边距和宽度以及填充,以便这两个框完全相互重叠。

这是代码

    /// <summary>
    /// Function to be executed when focus is on the password box.
    /// Basic function is to show the watermark in the password box.
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">RoutedEventArgs</param>
    private void PasswordBox_LostFocus(object sender, RoutedEventArgs e)
    {
        CheckPasswordWatermark();
    }

    /// <summary>
    /// Code checking the status of the password box and managing the watermark.
    /// </summary>
    private void CheckPasswordWatermark()
    {
        var passwordEmpty = string.IsNullOrEmpty(PwdBox.Password);
        PasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
        PwdBox.Opacity = passwordEmpty ? 0 : 100;
    }

    /// <summary>
    /// Function to be executed when the password box loses focus.
    /// Basic fuction is to show the watermark in the password box.
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">RoutedEventArgs</param>
    private void PasswordBox_GotFocus(object sender, RoutedEventArgs e)
    {
        PasswordWatermark.Opacity = 0;
        PwdBox.Opacity = 100;
        if (!string.IsNullOrEmpty(PwdBox.Password))
        {
            PwdBox.SelectAll();
        }
    }

这就是我的工作方式。

答案 1 :(得分:0)

以下是带有水印的PasswordBox的开源实现:https://github.com/JoshClose/WindowsPhoneControls