目前,我在WPF应用程序中对PasswordBox使用以下附加行为。我可以使用UI设置密码,对这个值进行加密并存储在数据库中。当加载视图模型和设置绑定到SecureString的PasswordBox属性时,密码字段显示为空。(即使密码已成功加载)。
为了使PasswordBox在设置来自ViewModel的值时显示为已填充,我需要更改什么?
我已经添加了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"/>ViewModel
public SecureString Password
{
get { return this._password; }
set { base.SetProperty(ref _password, value); }
}
public SettingsViewModel()
{
this.Password = LoadPassword();
}发布于 2016-04-22 06:57:19
您只是不能设置SecurePassword。它是一个readOnly属性。如果要填充PasswordBox,则必须设置不安全字符串密码属性。
https://stackoverflow.com/questions/36775127
复制相似问题