我在WPF中使用密码框,当我处于编辑模式时,密码框不会掩盖我的密码。
以下是我的代码示例。
<PasswordBox Width="200" Password="{Binding Path=Password, ValidatesOnDataErrors=True, NotifyOnValidationError=True, Mode=TwoWay}" HorizontalAlignment="Left"/>
我尝试过包括Passwordchar =&#39; *&#39;
答案 0 :(得分:2)
由于安全目的,您无法在viewmodel中直接获取密码值,因此您必须创建依赖属性。
下面是密码的代码。
DP:
public static readonly DependencyProperty BoundPassword =
DependencyProperty.RegisterAttached("BoundPassword", typeof(string), typeof(PasswordBoxHelper), new PropertyMetadata(string.Empty, OnBoundPasswordChanged));
public static readonly DependencyProperty BindPassword = DependencyProperty.RegisterAttached(
"BindPassword", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, OnBindPasswordChanged));
private static readonly DependencyProperty UpdatingPassword =
DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false));
private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var box = d as PasswordBox;
if(d == null || !GetBindPassword(d))
{
return;
}
box.PasswordChanged -= HandlePasswordChanged;
var newPassword = (string)e.NewValue;
if(!GetUpdatingPassword(box))
{
box.Password = newPassword;
}
box.PasswordChanged += HandlePasswordChanged;
}
private static void OnBindPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
var box = dp as PasswordBox;
if(box == null)
{
return;
}
bool wasBound = (bool)(e.OldValue);
bool needToBind = (bool)(e.NewValue);
if(wasBound)
{
box.PasswordChanged -= HandlePasswordChanged;
}
if(needToBind)
{
box.PasswordChanged += HandlePasswordChanged;
}
}
private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
{
var box = sender as PasswordBox;
SetUpdatingPassword(box, true);
SetBoundPassword(box, box.Password);
SetUpdatingPassword(box, false);
}
public static void SetBindPassword(DependencyObject dp, bool value)
{
dp.SetValue(BindPassword, value);
}
public static bool GetBindPassword(DependencyObject dp)
{
return (bool)dp.GetValue(BindPassword);
}
public static string GetBoundPassword(DependencyObject dp)
{
return (string)dp.GetValue(BoundPassword);
}
public static void SetBoundPassword(DependencyObject dp, string value)
{
dp.SetValue(BoundPassword, value);
}
private static bool GetUpdatingPassword(DependencyObject dp)
{
return (bool)dp.GetValue(UpdatingPassword);
}
private static void SetUpdatingPassword(DependencyObject dp, bool value)
{
dp.SetValue(UpdatingPassword, value);
}
以下是XAML:
<PasswordBox x:Name="PasswordBox"
utility:PasswordBoxHelper.BindPassword="true"
utility:PasswordBoxHelper.BoundPassword="{Binding Path=Password,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
答案 1 :(得分:1)
由于安全性,您无法在PasswordBox中使用Binding to password字段。 实际上你可以,但你的密码将存储在内存中。 试试这个http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html
答案 2 :(得分:0)
@JRB和@BorisErmako答案都可能有效,但不知何故重量级。
您不需要双向数据绑定,对吗?在大多数情况下,您不想从代码中更新密码字段,对吗?我想你不会。
然后,只需使用一个事件来获得通知。灵感来自Bind a WPF passwordbox我做了:
<PasswordBox PasswordChanged="OnPasswordChanged" />
假设代码隐藏类具有指向底层模型对象的指针myViewModelObject
。
private void OnPasswordChanged(object sender, RoutedEventArgs e)
{
myViewModelObject.Password = ((PasswordBox)sender).Password;
}
与其他答案的89到108个额外行相比,这增加了很少的代码行。额外奖励:出于所有实际目的,这似乎与ReactiveUI等高级框架完全兼容。
答案 3 :(得分:0)
要在WPF中使用带掩码的密码文本框,请实施以下方法:
类构造函数:
public Login_Form()
{
InitializeComponent();
Password_Text_Box.MaxLength = 7; // password 7 characters in length
AutoValidate = AutoValidate.Disable;
} // End of Login form initialization
组件验证:
private void Password_Text_Box_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
bool cancel = false;
if (string.IsNullOrEmpty(Password_Text_Box.Text)) {
cancel = true;
ErrorProvider.SetError(Password_Text_Box, " MISSING PASSWORD "); }
e.Cancel = cancel; }
“按键”操作背后的逻辑:
private void Password_Text_Box_KeyPress(object sender, KeyPressEventArgs e) {
if (e.KeyChar == (char)Keys.Enter) {
// if ENTER is hit , do something
e.Handled = true; //Handle the Keypress event (suppress the Beep) }
Password_Text_Box.PasswordChar = '*'; // masking input right away
e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back);
e = null;} // not allowing certain types of input
您还可以实施象形图来显示和隐藏输入的序列:
private void pictureBoxShowPassword_Click(object sender, EventArgs e) {
if (Password_Text_Box.PasswordChar == '*' & Password_Text_Box.Text != null) {
Password_Text_Box.PasswordChar = '\0'; } //revealing
else if (Password_Text_Box.PasswordChar == '\0'){
Password_Text_Box.PasswordChar = '*'; } } // hiding
当您指向并单击文本框时会发生什么:
private void Password_Text_Box_MouseDown(object sender, MouseEventArgs e) {
if (Password_Text_Box.Text == "Password") {
Password_Text_Box.Text = ""; } }
总而言之,您应该遵循以下思路:
加载表单和文本框时,它将显示文本-组件的用途。 当用户指向鼠标并单击组件时,目的文本消失。 输入值后,屏幕上会出现一个遮罩符号。 通过“显示”按钮附近的,您可以在提交之前看到输入的值。