Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a TextBox be a "password box" and display stars when using MVVM?

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.

like image 808
Edward Tanguay Avatar asked Jul 13 '09 13:07

Edward Tanguay


People also ask

How to use TextBox as PasswordBox in wpf?

WPF comes with a built-in PasswordBox control that allows you to handle and manage passwords in a WPF application. A PasswordBox control as a TextBox control with masking feature enabled. The PasswordBox control allows you to hide the characters and limit the number of characters to be typed in the editable area.

What is the use of a password box?

A password box is a text input box that conceals the characters typed into it for the purpose of privacy. A password box looks like a text box, except that it renders placeholder characters in place of the text that has been entered.


1 Answers

To get or set the Password in a PasswordBox, use the Password property. Such as

string password = PasswordBox.Password; 

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.

like image 105
Brandon Avatar answered Sep 28 '22 09:09

Brandon