WPF PasswordBox does not support binding workarounds

Source: Internet
Author: User

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

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: info-contact@alibabacloud.com 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.