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

Share:
12,111

Related videos on Youtube

NoIdea123
Author by

NoIdea123

Updated on June 04, 2022

Comments

  • NoIdea123
    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.
      Chris W. over 7 years
      Not sure what you mean, the property is Password and you can pass a string to it.
    • NoIdea123
      NoIdea123 over 7 years
      When 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
      Alejandro over 7 years
      Possible duplicate of How to bind to a PasswordBox in MVVM
    • NoIdea123
      NoIdea123 over 7 years
      Already checked this topic. For me it would be enough to have a Textbox without plaintext.. So it would be pretty more easy to handle