Search code examples
c#wpfxamlpasswordbox

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?


Solution

  • 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