How to make a WPF TextBox use password characters?

60,133

Solution 1

You have to use PasswordBox instead of TextBox:

<PasswordBox Height="42" Width="200"  Margin="22,28,28,0" 
         Name="passwordBox1" VerticalAlignment="Top" 
         Background="LightBlue" Foreground="DarkBlue" 
         MaxLength="25" PasswordChar="*" 
         />

Solution 2

There's a new control in WPF designed for passwords, it's called PasswordBox. You should use that instead of a TextBox if you need to mask the input.

Here's a brief article about it. To retrieve the value that was entered, use the Password property.

EDIT: You've pretty much asked a new question - how can you unmask the text in a WPF PasswordBox? To the best of my knowledge you can't, though you could of course display it in a regular TextBox on demand by getting the value of the password from PasswordBox.Password

Databinding to a PasswordBox isn't possible without a custom helper class - though this would reduce the increased security offered by the new PasswordBox control (as described here). With that considered, this article includes a section on creating a helper class that allows you to databind to a PasswordBox.

Solution 3

Or use 'password font'. A font that shows all characters as dots. Remember to disable cut and copy clipboard functions.

Solution 4

There are also custom implementations of PasswordBox which you can alter for your needs, see this article. You also can extend this which is not possible with the PasswordBox because its a sealed class.

Share:
60,133
pvaju896
Author by

pvaju896

** aim for solution..!! **

Updated on June 30, 2020

Comments

  • pvaju896
    pvaju896 almost 4 years

    i need to set it it dynamicaliy..

    Can i make password Box to as normal text- i mean- user could see the text what he entered.???

    its for-> i need to use same control for " password sesion" and also the "item count entering" session ..??