Hi people stackoverflow. I'm working with MVVM, I have ViewModel call UserViewModel with a Property Password. In the View have a control PasswordBox.
<PasswordBox x:Name="txtPassword" Password="{Binding Password}" />
But this xaml don't work. How do you do the binding?? Help please!!
There is an issue with the BindablePasswordBox. It only works in one direction, PasswordBox to PasswordProperty. Below is a modified version of it that works in both directions. It registers a PropertyChangedCallback and updates the PasswordBox's Password when it is called. I hope that someone finds this useful.
Check another thread on password box. Its better not to keep the password on any DP or public property.
Other thread on passwordbox
For security reasons the Password property is not a dependency property and therefore you can't bind to it. Unfortunately you'll need to perform the binding in the code behind the old fashioned way (register for OnPropertyChanged event and update the value through code...)
I quick search brings me to this blog post which shows how to write an attached property to sidestep the issue. Whether this is worth doing or not though really depends on your aversion to code-behind.
You can always write a control that wraps the Password and adds a dependency property for the Password property.
I would just use code behind, but if you must you can do something like:
To avoid having the password available in memory as plain text at any point, I provide the value as a parameter to my command.
Then in my view model.
While it would make sense to provide the SecurePassword directly from the binding, it always seems to provide an empty value. So this does NOT work: