When you want to extend the control, you find that PasswordBox is a sealed class and is not inheritable.
This function is implemented by attaching properties.
Two additional properties declared
ispasswordbindingenabled: Used to indicate whether a password can be used for binding, a subscription when a value changes, or a unsubscribe when password changes the password attached property for the binding
bindedpassword: The password used for binding, synchronized with the password, when the value change is synchronized with password
/// <summary> ///Extended Password box password can be used for binding helper classes/// </summary> Public classBindedpasswordhelper {#regionAdditional property that indicates whether the password for the password box can be bound Public Static BOOLgetispasswordbindingenabled (DependencyObject obj) {return(BOOL) obj. GetValue (Ispasswordbindingenabledproperty); } Public Static voidsetispasswordbindingenabled (DependencyObject obj,BOOLvalue) {obj. SetValue (ispasswordbindingenabledproperty, value); } Public Static ReadOnlyDependencyProperty Ispasswordbindingenabledproperty =dependencyproperty.registerattached ("ispasswordbindingenabled",typeof(BOOL), typeof(bindedpasswordhelper),NewUIPropertyMetadata (false, onispasswordbindingenabledchanged)); Private Static voidonispasswordbindingenabledchanged (DependencyObject obj, DependencyPropertyChangedEventArgs e) {varPasswordBox = obj asPasswordBox; if(PasswordBox! =NULL) {passwordbox.passwordchanged-=passwordboxpasswordchanged; if((BOOL) {e.newvalue) {passwordbox.passwordchanged+=passwordboxpasswordchanged; } } } //update the password for binding when the password changes Private Static voidPasswordboxpasswordchanged (Objectsender, RoutedEventArgs e) { varPasswordBox =(PasswordBox) sender; if(!string. Equals (Getbindedpassword (PasswordBox), Passwordbox.password)) {Setbindedpassword (PasswordBox, p Asswordbox.password); } } #endregion #regionAdditional properties for the password to bind Public Static stringGetbindedpassword (DependencyObject obj) {return(string) obj. GetValue (Bindedpasswordproperty); } Public Static voidSetbindedpassword (DependencyObject obj,stringvalue) {obj. SetValue (bindedpasswordproperty, value); } Public Static ReadOnlyDependencyProperty Bindedpasswordproperty =dependencyproperty.registerattached ("Bindedpassword",typeof(string), typeof(bindedpasswordhelper),NewUIPropertyMetadata (string. Empty, onbindedpasswordchanged)); //update password when bound password is updated Private Static voidonbindedpasswordchanged (DependencyObject obj, Dependencypropertych Angedeventargs e) {varPasswordBox = obj asPasswordBox; if(PasswordBox! =NULL) {Passwordbox.password= E.newvalue = =NULL?string. Empty:e.newvalue.tostring (); } } #endregion }
Using the method, it is important to note that, like a textbox, it is important to declare that mode is UpdateSourceTrigger, otherwise the bound value will not be synchronized with the Background property
<Style= "{StaticResource Passwordboxstyle}" Helper: bindedpasswordhelper.ispasswordbindingenabled= "True" Helper: Bindedpasswordhelper.bindedpassword= "{Binding password,mode=twoway,updatesourcetrigger= PropertyChanged}"/>
Password box password cannot bind problem solution