5

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?

NoIdea123
  • 141
  • 1
  • 2
  • 13
  • 2
    Not sure what you mean, the property is `Password` and you can pass a string to it. – Chris W. Dec 05 '16 at 19:01
  • 3
    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 – NoIdea123 Dec 05 '16 at 20:02
  • 1
    Possible duplicate of [How to bind to a PasswordBox in MVVM](http://stackoverflow.com/questions/1483892/how-to-bind-to-a-passwordbox-in-mvvm) – Alejandro Dec 05 '16 at 20:12
  • 1
    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 – NoIdea123 Dec 06 '16 at 15:14

1 Answers1

2

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

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42