The password box control is a special type of TextBox designed to enter passwords. The typed in characters are replaced by asterisks. Since the password box contains a sensible password it does not allow cut, copy, undo and redo commands.
To replace the asteriks character by another character, set the PasswordChar property to the character you desire.
When you try to databind the password property of a PasswordBox you will recognize that you cannot do data binding on it. The reason for this is, that the password property is not backed by a DependencyProperty.
The reason is databinding passwords is not a good design for security reasons and should be avoided. But sometimes this security is not necessary, then it's only cumbersome that you cannot bind to the password property. In this special cases you can take advantage of the following PasswortBoxHelper.
public static class PasswordHelper
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password",
typeof(string), typeof(PasswordHelper),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty AttachProperty =
DependencyProperty.RegisterAttached("Attach",
typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));
private static readonly DependencyProperty IsUpdatingProperty =
DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
typeof(PasswordHelper));
public static void SetAttach(DependencyObject dp, bool value)
{
dp.SetValue(AttachProperty, value);
}
public static bool GetAttach(DependencyObject dp)
{
return (bool)dp.GetValue(AttachProperty);
}
public static string GetPassword(DependencyObject dp)
{
return (string)dp.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject dp, string value)
{
dp.SetValue(PasswordProperty, value);
}
private static bool GetIsUpdating(DependencyObject dp)
{
return (bool)dp.GetValue(IsUpdatingProperty);
}
private static void SetIsUpdating(DependencyObject dp, bool value)
{
dp.SetValue(IsUpdatingProperty, value);
}
private static void OnPasswordPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
passwordBox.PasswordChanged -= PasswordChanged;
if (!(bool)GetIsUpdating(passwordBox))
{
passwordBox.Password = (string)e.NewValue;
}
passwordBox.PasswordChanged += PasswordChanged;
}
private static void Attach(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
if (passwordBox == null)
return;
if ((bool)e.OldValue)
{
passwordBox.PasswordChanged -= PasswordChanged;
}
if ((bool)e.NewValue)
{
passwordBox.PasswordChanged += PasswordChanged;
}
}
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
SetIsUpdating(passwordBox, true);
SetPassword(passwordBox, passwordBox.Password);
SetIsUpdating(passwordBox, false);
}
}
|
anand | |
|
Commented on 22.April 2009 |
good
|
|
|
|
Mohd. Irshad | |
|
Commented on 30.April 2009 |
Nice Article for a beginner hope u guys will continue more and do ur excelent so that we can learn.
|
|
|
|
Tore Aurstad | |
|
Commented on 10.May 2009 |
Plain and simple. Neat-o! Keep those clear tutorials coming, we love it!
|
|
|
|
Yasin | |
|
Commented on 14.May 2009 |
Hi Christian,
Thx for the tutorial, both the content and the style are really nice ;)
I've a question about this one,
when i copy+pasted these codes I got an error like this
''w' is an undeclared namespace. Line 8, position 26.' XML is not valid.
Am I missing sth ?
|
|
|
|
Christian Moser | |
|
Commented on 14.May 2009 |
Hi Yasin,
The article does not show the whole truth. The class PasswordHelper is defined somewhere in a namespace. You have to include this namespace into the XAML file where you want to use it. The syntax looks like this: xmlns:w="clr-namespace:Namespace.To.Passwordhelper". The "w" is just a sample prefix. You can replace it by what you want.
I hope this helpe.
Greetings
Christian
|
|
|
|
Yasin | |
|
Commented on 15.May 2009 |
Hi Christian,
Thanks a lot for your quick answer =)
I got it now ;)
I hope you'll also find help from others when you need it ;)
Yours respectfully.
Yasin.
|
|
|
|
Leonardo | |
|
Commented on 3.June 2009 |
Hi, I have written an article on PasswordBox and security: http://www.codeproject.com/KB/WPF/wpf_enhanced_passwordbox.aspx
|
|
|
|
Tan | |
|
Commented on 23.June 2009 |
Please help i have the error:
The attachable property 'Attach' was not found in type 'PasswordHelper'.
I just copyed the PasswordHelper class to a file namned PasswordHelper.cs
and then wrote the
xmlns:w="clr-namespace:Namespace.To.Passwordhelper".
|
|
|
|
Tan | |
|
Commented on 23.June 2009 |
Please help i have the error:
The attachable property 'Attach' was not found in type 'PasswordHelper'.
I just copyed the PasswordHelper class to a file namned PasswordHelper.cs
and then wrote the
xmlns:w="clr-namespace:Namespace.To.Passwordhelper".
|
|
|
|
Tan | |
|
Commented on 23.June 2009 |
Please help i have the error :
|
|
|
|
Tan | |
|
Commented on 23.June 2009 |
Sorry for many doulbe post please erase!! SORRY
|
|
|
|
Patel Dishant | |
|
Commented on 1.July 2009 |
Can I Create or call the css File in SilverLight
|
|
|
|
satya | |
|
Commented on 11.August 2009 |
I want like vista password textbox type , I meen initialy passwordtextbox contains "password",when the user going to enter his password it will be shown only "*"s like that..
|
|
|
|
mo | |
|
Commented on 8.September 2009 |
@satya: google for wpf watermark
@Tan: "Namespace.to.Passwordhepler" is a placeholder. Fill in your own Namespace and learn how to post
|
|
|
|
Manoj | |
|
Commented on 15.October 2009 |
How to run this code as I am very new to programming
|
|
|
|
Malu | |
|
Commented on 6.November 2009 |
Too Simple but lack content
|
|
|
|
Senthil | |
|
Commented on 18.November 2009 |
How to validate the passwordbox or any textbox for required validation and also for Regular expression validation.
|
|
|
|
Senthil | |
|
Commented on 18.November 2009 |
How to redirect to another page on clicking button and also how to check the password is correct as in sql database.
|
|
|
|
Senthil | |
|
Commented on 18.November 2009 |
I had create a page in which there are two text box and a button onclicking that button I have to save the value in textbox in database.
Can any one give the code for this scenario. I m new to WPF
|
|
|
|
Rajagopal | |
|
Commented on 19.November 2009 |
article is great...
|
|
|
|
sudha | |
|
Commented on 2.December 2009 |
Good article...
|
|
|
|
amit | |
|
Commented on 11.December 2009 |
good article
to reset password box text use
password1.clear();
|
|
|
|
harshad | |
|
Commented on 15.December 2009 |
can anybody please put the namespace required for the passwordhelper?
|
|
|
|
harshad | |
|
Commented on 15.December 2009 |
sorry my bad.......!!! i didnt see the class below.
|
|
|
|
Chinu | |
|
Commented on 24.December 2009 |
Good article...
|
|
|
|
Great | |
|
Commented on 4.January 2010 |
It explains well about the dependency property too :)
|
|
|
|
waqas | |
|
Commented on 5.January 2010 |
extremly superb introductory material..... very good effort.
|
|
|
|
kd | |
|
Commented on 7.April 2010 |
hi..in order to use this password textbox, do we need to add any references? because when i try to put PasswordBox in my page, there were an error.
|
|
|
|
PLM | |
|
Commented on 26.May 2010 |
As anyone manage to have that logic working in VB.NET? I am able to have that example working in C# but getting issue with VB.NET.
I have a PasswordHelper class that compiles but when I try to add the properties to my PasswordBox it does not recognize them (evenknow I have the correct namespace at the top of my UserControl)
|
|
|
|
PLM | |
|
Commented on 26.May 2010 |
As anyone manage to have that logic working in VB.NET? I am able to have that example working in C# but getting issue with VB.NET.
I have a PasswordHelper class that compiles but when I try to add the properties to my PasswordBox it does not recognize them (evenknow I have the correct namespace at the top of my UserControl)
|
|
|
|
rahul bhojane | |
|
Commented on 3.June 2010 |
Good article...
|
|
|
|
David | |
|
Commented on 24.June 2010 |
Very helpfull, though I had some trouble converting the code to c# only (no XAML)
still not sure how the PasswordHelper got attached, since I was not able to translate this line in C# :
PasswordBox w:PasswordHelper.Attach="True"
but it seems the property works fine so... guess the attachment is kind of transparent in my code, for whatever reason...
|
|
|
|
Taylor Leese | |
|
Commented on 25.July 2010 |
I created a similar bindable password box here (http://gist.github.com/468331) as well.
|
|
|
|
Sarah | |
|
Commented on 13.August 2010 |
Why is the Attach method private? It causes XAML to say "The attachable property 'Attach' was not found in type 'PasswordHelper'. Not very useful.
|
|
|
|
ravi | |
|
Commented on 19.August 2010 |
thi is very nice
|
|
|
|
wert | |
|
Commented on 25.August 2010 |
twerwetr
|
|
|
|
JKOne | |
|
Commented on 28.October 2010 |
The Input Panel in Windows 7 doesn't work with the PasswordBox within WPF projects. Does anyone have an idea how to handle the problem?
|
|
|
|
Normand Bedard | |
|
Commented on 21.December 2010 |
When using validation input on passwordbox (implementing IDataErrorInfo), the red line around the box appear, but the error message text when moving mouse over the passwordbox does not appear. Any idea?
|
|
|
|
Beth | |
|
Commented on 8.February 2011 |
is it possible to register event then raise it from OnPasswordPropertyChanged so that the code that attaches this helper class can do post processing?
|
|
|
|
Christo... | |
|
Commented on 15.February 2011 |
You're essentially making the password field a dependency property. Doesn't this just circumvent the original security related issues which was the intent of not having the password field a dependency property? Aren't you just making a very big security hole by doing this?
|
|
|
|
flyinghippie | |
|
Commented on 18.February 2011 |
right i am on on the 20th april if you want my pass i will tell you on there the price is 50 pounds :)
|
|
|
|
flying hippie | |
|
Commented on 18.February 2011 |
we will bid starting on 50 and on sever polor bear
|
|
|
|
flyinghippie | |
|
Commented on 18.February 2011 |
on cp tell ur kids
|
|
|
|
ameen sheriff | |
|
Commented on 24.February 2011 |
nice.. concentrate
|
|
|
|
Siavash... | |
|
Commented on 16.March 2011 |
Thanks, you saved my day ;)
|
|
|
|
Daniel | |
|
Commented on 16.March 2011 |
Great, thank you...
|
|
|
|
Divya | |
|
Commented on 23.March 2011 |
Good
|
|
|
|
Jared Barneck | |
|
Commented on 9.April 2011 |
I am using this code, but I ran into a problem. I am also using Pages and NavigationService. Turns out the NavigationService blanks this password.
So I have a call to the navigation service, and I have to save the password, and replace it afterwards. Thought I would let you know.
Here are the details:
WPF NavigationService blanks PasswordBox.Password, which breaks the MVVM PasswordHelper
http://www.rhyous.com/2011/04/08/wpf-navigationservice-blanks-passwordbox-password-which-breaks-the-mvvm-passwordhelper/
|
|
|
|
ALI | |
|
Commented on 24.April 2011 |
Thanks,Can you help me about text block formatting?
I want to use approximation in text block contents,for example I have a double number like 14.34563466456 and I want to show this number in 2 float, means 14.34; how can I do it? send answer to my mail please.Thanks alot and Good Luck!
email: ALI_KHALAJI64@YAHOO.COM
|
|
|
|
karle | |
|
Commented on 29.June 2011 |
For my understanding this code can never run, because calling SetPassword will reset the binding. I see this in the debugger. (?!)
|
|
|
|
VINIT KUMAR | |
|
Commented on 30.June 2011 |
ur artical is superub Thank's for it. I want to know that cant we set a textbox's input mode to password...???
|
|
|
|
Kikoo | |
|
Commented on 21.July 2011 |
Great article! You made my day, thanks!!!
|
|
|
|
Praveen Gupta | |
|
Commented on 31.July 2011 |
good code for help me thanks
|
|
|
|
Abraham | |
|
Commented on 2.August 2011 |
good work ;)
|
|
|
|
Joe | |
|
Commented on 2.August 2011 |
Took me a while to figure this out, but here is a bit more complete bit of XAML showing how to bind. "UserPass" is the dependency property on the backend that I bind to:
<StackPanel>
<PasswordBox h:PasswordHelper.Attach="True" h:PasswordHelper.Password="{Binding Text, ElementName=plain, Mode=TwoWay}" Width="150" />
<TextBlock x:Name="plain" Height="0" Text="{Binding UserPass, Mode=TwoWay}"/>
</StackPanel>
|
|
|
|
Ramya | |
|
Commented on 17.September 2011 |
Hi I'm new to this environment could u please help me for the data binding properties of passwordbox,grid,calendar controls. Thanks in advance:)
|
|
|
|
Rakesh | |
|
Commented on 21.September 2011 |
that's good
|
|
|