Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind a WPF passwordbox

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?

like image 402
NoIdea123 Avatar asked Oct 30 '22 15:10

NoIdea123


1 Answers

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
like image 158
Petter Hesselberg Avatar answered Nov 11 '22 18:11

Petter Hesselberg