The password property of PasswordBox does not support direct binding for security reasons, and can be implemented using dependency properties. Inserting code directly
public class Passwordboxhelper {public static readonly DependencyProperty Passwordproperty = Depende Ncyproperty.registerattached ("Password", typeof (String), typeof (Passwordboxhelper), new Frameworkpro Pertymetadata (String. Empty, onpasswordpropertychanged)); public static readonly DependencyProperty Attachproperty = dependencyproperty.registerattached ("Attach", typeof (BOOL), typeof (Passwordboxhelper), new PropertyMetadata (False, Attach)); private static readonly DependencyProperty Isupdatingproperty = dependencyproperty.registerattached ("isupdating" , typeof (BOOL), typeof (Passwordboxhelper)); public static void Setattach (DependencyObject DP, BOOL value) {DP. SetValue (attachproperty, value); public static bool Getattach (DependencyObject DP) {return (BOOL) DP. GetValue (Attachproperty); } public static string GetPassword (DEpendencyobject dp) {return (string) DP. GetValue (Passwordproperty); public static void SetPassword (DependencyObject dp, String value) {DP. SetValue (passwordproperty, value); } private static bool Getisupdating (DependencyObject DP) {return (BOOL) DP. GetValue (Isupdatingproperty); } private static void Setisupdating (DependencyObject DP, BOOL value) {DP. SetValue (isupdatingproperty, value); } private static void Onpasswordpropertychanged (DependencyObject sender, Dependencypropertychangedeventa RGS e) {PasswordBox PasswordBox = sender as PasswordBox; Passwordbox.passwordchanged-= passwordchanged; if (! ( BOOL) getisupdating (PasswordBox)) {Passwordbox.password = (string) e.newvalue; } passwordbox.passwordchanged + = passwordchanged; } private static void ATtach (DependencyObject sender, DependencyPropertyChangedEventArgs e) {PasswordBox Passwordb Ox = sender as PasswordBox; if (PasswordBox = = null) return; if (bool) e.oldvalue) {passwordbox.passwordchanged-= passwordchanged; } if ((bool) e.newvalue) {passwordbox.passwordchanged + = passwordchanged; }} private static void PasswordChanged (object sender, RoutedEventArgs e) {PasswordBox P Asswordbox = sender as PasswordBox; Setisupdating (PasswordBox, true); SetPassword (PasswordBox, Passwordbox.password); Setisupdating (PasswordBox, false); } }
Use:
<passwordbox helper:passwordhelper.attach= "True" helper:passwordhelper.password= "{Binding Model.Password, Mode= TwoWay, updatesourcetrigger=propertychanged} "> <PasswordBox.InputBindings> <keybinding key=" Enter "command=" {Binding logincommand} "/> </PasswordBox.InputBindings> </PasswordBox>
WPF PasswordBox does not support binding workarounds