2

元素的 Password 属性PasswordBox不是一个DependencyObject所以不能绑定(没有 Text 属性)。通过将消息附加到PasswordChanged事件,传递源并在视图模型中获取它,可以在用户键入时捕获密码。然而,缺乏绑定意味着没有机会设置ValidatesOnDataErrors=True,因此IDataErrorInfo永远不会调用方法,这意味着永远不会激活任何错误模板。有谁知道如何解决这个问题?也就是说,是否有另一种方法可以让元素调用IDataErrorInfo索引器的视图模型?

一个合理的问题可能是您为什么要验证密码。

两个原因:

1) 让用户知道密码是必需的。

2)让用户知道密码和密码确认是否相同。

4

2 回答 2

0

这也是一个很好的回答:http ://wpftutorial.net/PasswordBox.html 但是,如果您这样做,密码将作为纯文本存储在内存中。

也许最好将示例中的附加属性“Password”更改为“IsEmpty”和“PasswordHash”之类的东西。

于 2013-05-24T14:48:35.230 回答
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);
    }
}
于 2013-05-24T15:30:41.663 回答