Bind a WPF passwordbox

NoIdea123 picture NoIdea123 · Dec 5, 2016 · Viewed 11k times · Source

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?

Answer

Petter Hesselberg picture Petter Hesselberg · Dec 9, 2016

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