Bind a WPF passwordbox
12,111
The Password
property is not a dependency property -- for security reasons. You can easily get the plain-text password, though.
XAML:
<PasswordBox
x:Name="passwordBox"
PasswordChanged="OnPasswordChanged" />
Code-behind event handler:
private void OnPasswordChanged(
object sender,
RoutedEventArgs e)
{
Debug.WriteLine(passwordBox.Password);
}
Updates
- Samuel Jack describes how to bind to a
PasswordBox
using attached properties - A discussion on security risk
Related videos on Youtube
Author by
NoIdea123
Updated on June 04, 2022Comments
-
NoIdea123 almost 2 years
I want to bind a Passwordbox in the XAML code. Now I found out that it isn´t possible to bind it because you haven´t any dependency property.
For my case I don´t need any security in my Application. I just don´t want to display clear text to the user. Is there any other option to realize a kind of passwordbox with a textbox or so on?
-
Chris W. over 7 yearsNot sure what you mean, the property is
Password
and you can pass a string to it. -
NoIdea123 over 7 yearsWhen i set my Binding like this:
Password="{Binding MyPassword}"
i got the error"A binding cannot be set on the password property of type "passwordbox". A binding can only be set on a dependency property of a dependency object."
In this MyPassword is a StringProperty -
Alejandro over 7 yearsPossible duplicate of How to bind to a PasswordBox in MVVM
-
NoIdea123 over 7 yearsAlready checked this topic. For me it would be enough to have a Textbox without plaintext.. So it would be pretty more easy to handle
-