I\'ve read that the password in a WPF PasswordBox does not have a dependency property for binding the password for security reasons. Despite this, there are ways to
Using tools like WPF Inspector or Snoop you can spy the password string. An alternative to passing the PasswordBox to the view-model is to attach a Behavior
public sealed class PasswordBoxBehavior : Behavior
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.LostKeyboardFocus += AssociatedObjectLostKeyboardFocus;
}
protected override void OnDetaching()
{
AssociatedObject.LostKeyboardFocus -= AssociatedObjectLostKeyboardFocus;
base.OnDetaching();
}
void AssociatedObjectLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var associatedPasswordBox = AssociatedObject as PasswordBox;
if (associatedPasswordBox != null)
{
// Set your view-model's Password property here
}
}
}
and the XAML code:
...
...