How can I do this in XAML:
PSEUDO-CODE:
<TextBox Text="{Binding Password}" Type="Password"/>
so that the user sees stars or dots when he is typing in the password.
I've tried various examples which suggest PasswordChar and PasswordBox but can't get these to work.
e.g. I can do this as shown here:
<PasswordBox Grid.Column="1" Grid.Row="1"
PasswordChar="*"/>
but I of course want to bind the Text property to my ViewModel so I can send the value the bound TextBox when the button is clicked (not working with code behind), I want to do this:
<TextBox Grid.Column="1" Grid.Row="0"
Text="{Binding Login}"
Style="{StaticResource FormTextBox}"/>
<PasswordBox Grid.Column="1" Grid.Row="1"
Text="{Binding Password}"
PasswordChar="*"
Style="{StaticResource FormTextBox}"/>
But PasswordBox doesn't have a Text property.
To get or set the Password in a PasswordBox, use the Password property. Such as
This doesn't support Databinding as far as I know, so you'd have to set the value in the codebehind, and update it accordingly.
I did the below from the Views codebehind to set my property within the view model. Not sure if it really " breaks" the MVVM pattern but its the best and least complexe solution found.
The problem with using the PasswordBox is that it is not very MVVM friendly due to the fact that it works with SecureString and therefore requires a shim to bind it to a String. You also cannot use the clipboard. While all these things are there for a reason, you may not require that level of security. Here is an alternative approach that works with the clipboard, nothing fancy. You make the TextBox text and background transparent and bind the text to a TextBlock underneath it. This textblock converts characters to * using the converter specified.
And here is the Value Converter:
Make sure your Text property on your viewmodel implements INotifyPropertyChanged
Thanks Cody, that was very helpful. I've just added an example for guys using the Delegate Command in C#
There's a way to bind on a PasswordBox here: PasswordBox Databinding
You can make your
TextBox
as customedPasswordBox
by simply adding the following value toFontFamily
property of yourTextBox
control.In my case this works perfectly. This will show dot in place of the actual text (not star(*) though).