1

I'm aware that you can't Bind to PasswordBox.Password in WPF. How do I handle a form that contains one? All other controls on the form are bound to properties on a business object, but I have to manually set and retrieve a password property whenever the input changes.

I know that others have created custom controls that allow binding to this property, but, on the other hand, Microsoft explicitly disallows binding to this property for a reason.

Are there any elegant solutions to this problem, or am I stuck writing code to bridge the gap between my form and my business object?

Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149

2 Answers2

1

Just bind to PasswordBox itself, i.e. {Binding ElementName=MyPasswordBox}.

1

It is a repeat of this also, and there you can find two other approaches: one (in the question) with a PasswordBox public property on the ViewModel (I dislike this approach), and one (in the accepted answer) with and event handler and a SecureString property on the ViewModel. It's a bit "traditional", but I prefer this approach instead of writing many lines of code to bind a password with an Attached Property. Hope this helps.

Community
  • 1
  • 1
Daniele Armanasco
  • 7,289
  • 9
  • 43
  • 55