WPF MVVM PasswordBox绑定

时间:2016-04-21 16:10:38

标签: c# wpf mvvm

我目前在WPF应用程序的PasswordBox上使用以下附加行为。我可以使用UI设置密码,我加密这个值并存储在数据库中。 加载视图模型并设置绑定到PasswordBox的SecureString属性时,密码字段显示为空。 (即使密码已成功加载)。

从ViewModel设置值时,为了让PasswordBox显示为已填充,我需要更改哪些内容?

我添加了XAML和ViewModel中使用的代码。 LoadPassword()方法返回SecureString。

行为

/// <summary>
/// Provides additional properties to use against PasswordBox controls.
/// </summary>
/// <remarks>
/// Sometimes controls have properties that don't support binding. PasswordBox is one of these, where Microsoft's implementation does not support binding against the entered
/// password (for 'security purposes').
/// 
/// This class provides an 'attached property' which allows us to bridge the gap between the view and view model.
/// </remarks>
public static class Password
{
    #region Dependency properties

    /// <summary>
    /// SecurePassword property.
    /// </summary>
    public static readonly DependencyProperty SecurePasswordProperty = DependencyProperty.RegisterAttached("SecurePassword", typeof(SecureString), typeof(Password), new FrameworkPropertyMetadata(OnSecurePasswordChanged));

    /// <summary>
    /// HasSecurePassword property.
    /// </summary>
    private static readonly DependencyProperty HasSecurePasswordProperty = DependencyProperty.RegisterAttached("HasSecurePassword", typeof(bool), typeof(Password));

    #endregion

    #region Methods

    /// <summary>
    /// Sets the value of the SecurePassword property.
    /// </summary>
    /// <param name="dependencyObject">The object to set the property for.</param>
    /// <param name="value">The value to set.</param>
    public static void SetSecurePassword(DependencyObject dependencyObject, SecureString value)
    {
        if (dependencyObject == null)
            throw new ArgumentNullException(nameof(dependencyObject));

        dependencyObject.SetValue(Password.SecurePasswordProperty, value);
    }

    /// <summary>
    /// Gets the value of the SecurePassword property.
    /// </summary>
    /// <param name="dependencyObject">The object to get the property for.</param>
    /// <returns>The current value.</returns>
    public static SecureString GetSecurePassword(DependencyObject dependencyObject)
    {
        if (dependencyObject == null)
            throw new ArgumentNullException(nameof(dependencyObject));

        return (SecureString)dependencyObject.GetValue(Password.SecurePasswordProperty);
    }

    #endregion

    /// <summary>
    /// Handles the SecurePassword value changing.
    /// </summary>
    private static void OnSecurePasswordChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        PasswordBox password = dependencyObject as PasswordBox;

        bool? isRegistered = (bool?)password?.GetValue(Password.HasSecurePasswordProperty);
        if (isRegistered == false)
        {
            // register with the PasswordBox's PasswordChanged event so that we can keep updated with the latest value entered by the user
            password.PasswordChanged += (s, ee) => SetSecurePassword(password, password.SecurePassword);
            password.SetValue(Password.HasSecurePasswordProperty, true);
        }
    }
}

XAML

<PasswordBox Grid.Column="1" Grid.Row="2" behaviours:Password.SecurePassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="2"/>

视图模型

public SecureString Password
{
    get { return this._password; }
    set { base.SetProperty(ref _password, value); }
}

public SettingsViewModel()
{
    this.Password = LoadPassword();
}

1 个答案:

答案 0 :(得分:1)

您根本无法设置SecurePassword。这是一个readOnly属性。如果要填充PasswordBox,则必须设置不安全字符串Password属性。