PasswordBox Binding

djschwartz picture djschwartz · May 20, 2009 · Viewed 25.1k times · Source

I'm just getting started with M-V-VM and WPF and having issues understanding some binding issues.

I have a login page that has a ComboBox and a PasswordBox. The ComboBox looks like this:

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

This works just fine - my values get updated everytime the SelectedItem changes on the ComboBox!

In my ViewModel I have an ICommand which uses this method to determine if the Login button is active:

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

So my problem is I don't have the PasswordBox bound to the Password property on the ViewModel - so I have no way to tell when it is updated.

So how do I get the value of the PasswordBox to my ViewModel? Everything I've read just says don't bind a PasswordBox for security reasons. I would simply take off the password restriction on the CanLogin() but I need the value to pass along to an AccountService.

Answer

ArielBH picture ArielBH · May 20, 2009

Interesting.

look at this blog post and see if it is helping you. http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html

Apparently the link is dead now so here is the original solution (found here):

You can use attached properties to create a helper like this:

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

Use it:

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