在密码框的某些事件上显示密码字符 [英] showing password characters on some event for passwordbox
问题描述
我正在开发一个 Windows 手机应用程序.我要求用户登录.
I am developing a windows phone application.In that i ask the user to login.
在登录页面上,用户必须输入密码.
On the login page the user has to enter password.
现在我想要的是给用户一个复选框,选中时应显示密码字符.
Now what I want is that i give user a check box which when selected should show the characters of the password.
我没有在密码框上看到任何显示密码字符的属性.
I have not seen any property on password box to show password characters.
请建议一些方法来做到这一点.
Please suggest some way to do it.
推荐答案
不要认为 PasswordBox 可以实现...只是一个想法,但是您可以使用隐藏的 TextBox 并在用户单击CheckBox,你只需隐藏PasswordBox并显示TextBox;如果他再次点击,你会再次切换他们的可见性状态,依此类推...
Don't think that is possible with PasswordBox... just a thought, but you might accomplish the same result using a hidden TextBox and when the user clicks the CheckBox, you just hide the PasswordBox and show the TextBox; if he clicks again, you switch their Visibility state again, and so on...
编辑
这就是方法!
只需添加一个页面,将 ContentPanel 更改为 StackPanel 并添加此 XAML 代码:
Just add a page, change the ContentPanel to a StackPanel and add this XAML code:
<PasswordBox x:Name="MyPasswordBox" Password="{Binding Text, Mode=TwoWay, ElementName=MyTextBox}"/>
<TextBox x:Name="MyTextBox" Text="{Binding Password, Mode=TwoWay, ElementName=MyPasswordBox}" Visibility="Collapsed" />
<CheckBox x:Name="ShowPasswordCharsCheckBox" Content="Show password" Checked="ShowPasswordCharsCheckBox_Checked" Unchecked="ShowPasswordCharsCheckBox_Unchecked" />
接下来,在页面代码上,添加以下内容:
Next, on the page code, add the following:
private void ShowPasswordCharsCheckBox_Checked(object sender, RoutedEventArgs e)
{
MyPasswordBox.Visibility = System.Windows.Visibility.Collapsed;
MyTextBox.Visibility = System.Windows.Visibility.Visible;
MyTextBox.Focus();
}
private void ShowPasswordCharsCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
MyPasswordBox.Visibility = System.Windows.Visibility.Visible;
MyTextBox.Visibility = System.Windows.Visibility.Collapsed;
MyPasswordBox.Focus();
}
这很好用,但如果再多做一些工作,您就可以完全使用 MVVM 了!
This works fine, but with a few more work, you can do this fully MVVM'ed!
这篇关于在密码框的某些事件上显示密码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!