我想知道是否有人有任何好主意如何显示密码在密码盒。我知道你可以将文本框绑定到密码盒,但这不是另一种方法吗?
如果你想让你的密码可见,你应该使用一个文本框。密码框的唯一功能是屏蔽输入。它不提供额外的功能。也可以在运行时切换文本框和密码框
在PasswordBox控件中没有任何内置属性来显示密码字符。但是我们可以通过TextBox控件在PasswordBox中显示密码。用于WPF中具有显示/隐藏功能的密码框。我们将使用两个TextBlock控件,TextBox控件,PasswordBox控件,Image控件
<标题> XAML h1> 背后的代码string AppPath = Directory.GetCurrentDirectory();
public MainWindow()
{
InitializeComponent();
ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\img\clip.jpg"));
}
private void ImgShowHide_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
HidePassword();
}
private void ImgShowHide_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
ShowPassword();
}
private void ImgShowHide_MouseLeave(object sender, MouseEventArgs e)
{
HidePassword();
}
private void txtPasswordbox_PasswordChanged(object sender, RoutedEventArgs e)
{
if(txtPasswordbox.Password.Length > 0)
ImgShowHide.Visibility = Visibility.Visible;
else
ImgShowHide.Visibility = Visibility.Hidden;
}
void ShowPassword()
{
ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\img\cus.jpg"));
txtVisiblePasswordbox.Visibility = Visibility.Visible;
txtPasswordbox.Visibility = Visibility.Hidden;
txtVisiblePasswordbox.Text = txtPasswordbox.Password;
}
void HidePassword()
{
ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\img\clip.jpg"));
txtVisiblePasswordbox.Visibility = Visibility.Hidden;
txtPasswordbox.Visibility = Visibility.Visible;
txtPasswordbox.Focus();
}
标题>
这里有一个好而简单的方法,你可以从这里下载密码字体并在文本框中使用它作为字体,而不是passwordbox当你想显示密码时,你所要做的就是把字体从"Password"到"Microsoft Sans Serif"或者任何东西//例如,当我点击复选框:
Private Sub ChkShowPass_Click(sender As Object, e As RoutedEventArgs)
If ChkShowPass.IsChecked = True Then
'show Password
TxtPassword.FontFamily = New FontFamily("Microsoft Sans Serif")
ElseIf ChkShowPass.IsChecked = False Then
'hide Password
TxtPassword.FontFamily = New FontFamily("Password")
End If
End Sub