0
我目前在WPF應用程序的密碼框中使用以下附加行爲。我可以使用UI設置密碼,我加密這個值並存儲在數據庫中。 加載視圖模型並設置綁定到密碼框的SecureString屬性時,密碼字段顯示爲空。 (即使密碼已成功加載)。WPF MVVM密碼框綁定
爲了使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"/>
視圖模型
public SecureString Password
{
get { return this._password; }
set { base.SetProperty(ref _password, value); }
}
public SettingsViewModel()
{
this.Password = LoadPassword();
}
您可以包含屬性的PasswordBox XAML和ViewModel代碼嗎? –
@GlenThomas我添加了更多的代碼示例,它們顯示了更大的圖片。我原本應該包括他們,對此很抱歉。 – seanzi