PasswordBox
元素的Password属性不是DependencyObject
,因此无法绑定(没有Text属性)。通过将消息附加到PasswordChanged
事件,传递源并在视图模型中抓取它,可以在用户键入时捕获密码。但是,缺少绑定意味着没有机会设置ValidatesOnDataErrors=True
,因此IDataErrorInfo
方法永远不会调用意味着永远不会激活任何错误模板。有谁知道如何解决这个问题?也就是说,是否有另一种方法让元素调用IDataErrorInfo
索引器的视图模型?
一个合理的问题可能是您想要验证密码的原因。
有两个原因:
1)让用户知道密码是必需的。
2)让用户知道密码和密码确认是否相同。
答案 0 :(得分:0)
这也是一个好的下水道:http://wpftutorial.net/PasswordBox.html 但是,如果这样做,密码将作为纯文本存储在内存中。
也许那么最好将示例中的附加属性“密码”更改为“IsEmpty”和“PasswordHash”。
答案 1 :(得分:0)
除了下面Alex的回答,我也使用了本文中的信息;
http://wpftutorial.net/PasswordBox.html
我做了以下允许验证的地方;
<强> XAML:强>
<Grid Grid.Row="2" Grid.Column="1">
<TextBox x:Name="plain1" Text="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,NotifyOnValidationError=True}" Visibility="Hidden" />
<PasswordBox x:Name="PasswordHelperBox" w:PasswordHelper.Attach="True" w:PasswordHelper.Password="{Binding Text, ElementName=plain1, Mode=TwoWay}" TabIndex="2" Margin="0,2,30,2"/>
</Grid>
其中“密码”是要绑定的属性。
助手类;
public static class PasswordHelper
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password",
typeof(string), typeof(PasswordHelper),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty AttachProperty =
DependencyProperty.RegisterAttached("Attach",
typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));
private static readonly DependencyProperty IsUpdatingProperty =
DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
typeof(PasswordHelper));
public static void SetAttach(DependencyObject dp, bool value)
{
dp.SetValue(AttachProperty, value);
}
public static bool GetAttach(DependencyObject dp)
{
return (bool)dp.GetValue(AttachProperty);
}
public static string GetPassword(DependencyObject dp)
{
return (string)dp.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject dp, string value)
{
dp.SetValue(PasswordProperty, value);
}
private static bool GetIsUpdating(DependencyObject dp)
{
return (bool)dp.GetValue(IsUpdatingProperty);
}
private static void SetIsUpdating(DependencyObject dp, bool value)
{
dp.SetValue(IsUpdatingProperty, value);
}
private static void OnPasswordPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
passwordBox.PasswordChanged -= PasswordChanged;
if (!(bool)GetIsUpdating(passwordBox))
{
passwordBox.Password = (string)e.NewValue;
}
passwordBox.PasswordChanged += PasswordChanged;
}
private static void Attach(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
if (passwordBox == null)
return;
if ((bool)e.OldValue)
{
passwordBox.PasswordChanged -= PasswordChanged;
}
if ((bool)e.NewValue)
{
passwordBox.PasswordChanged += PasswordChanged;
}
}
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
SetIsUpdating(passwordBox, true);
SetPassword(passwordBox, passwordBox.Password);
SetIsUpdating(passwordBox, false);
}
}