PasswordBox绑定

时间:2009-05-20 15:00:38

标签: wpf mvvm binding passwordbox

我刚刚开始使用M-V-VM和WPF,并且遇到了解一些绑定问题的问题。

我的登录页面有ComboBoxPasswordBoxComboBox看起来像这样:

<ComboBox Name="comboBox1" SelectedItem="{Binding Path=Username}">

这很好用 - 每次SelectedItem ComboBox更改时,我的值都会更新!

在我的ViewModel中,我有一个ICommand,它使用此方法来确定“登录”按钮是否处于活动状态:

public bool CanLogin()
{
    return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password);
}

所以我的问题是我没有将PasswordBox绑定到ViewModel上的Password属性 - 所以我无法判断它何时更新。

那么如何将PasswordBox的值提供给我的ViewModel?我读过的所有内容都表示,出于安全原因,不要绑定PasswordBox。我只是取消了CanLogin()的密码限制,但我需要将值传递给AccountService。

2 个答案:

答案 0 :(得分:27)

有趣。

查看此博客文章,看看它是否对您有所帮助。 http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html

显然链接已经死了所以这是原始解决方案(找到here):

您可以使用附加属性来创建这样的帮助程序:

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);
    }
}

使用它:

<PasswordBox w:PasswordHelper.Attach="True" 
             w:PasswordHelper.Password="{Binding Text, ElementName=plain, Mode=TwoWay}" 
             Width="100"/>

答案 1 :(得分:8)

我发布了一个GIST here,它是一个可绑定的密码框。