Password box password cannot bind problem solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.