Mark Carpenter
Mark Carpenter

Reputation: 17775

How do I elegantly handle using a PasswordBox in WPF?

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?

Upvotes: 1

Views: 2496

Answers (2)

Daniele Armanasco
Daniele Armanasco

Reputation: 7451

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.

Upvotes: 1

Vladislav Borovikov
Vladislav Borovikov

Reputation: 128

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

Upvotes: 1

Related Questions