Using the PasswordBox control in MVVM encounters a problem. Because the **passwordbox.password** property is not a dependency property, it cannot be used as the target of a binding. # Use a solution with attached properties! [Password Demo.gif] (http://upload-images.jianshu.io/upload_images/140233-dbd415eb4cf9aeb2.gif) * * Idea: * * Definition of two dependency properties **attach** and * * When Attachpassoword**attatch is true, a subscriber is added to the PasswordBox passwordchanged event. In this Subscriber, the Passwordbox.password is assigned to this additional property using the Attachpassword's set wrapper. Then, in XAML, use the extension tag to Attachpassword as the target of the binding. * * Data stream:**passwordbox.password->attachedpasswordproperty<-> Textblock.textproperty, it can be found that there is a lack of a link, is Passwordbox.password<-attachedpasswordproperty, This can be implemented in the Attachedpasswordproperty callback method. Notice here that the addition of the validation mechanism, both in the code isupdating, if not added to this validation, will occur in the ring data flow (strictly speaking Passwordbox.password as the goal of the logical mulitbinnd)!! Causes a problem at the cursor position. "' Csharpusing system.windows;using System.Windows.Controls;
Namespace password binding {public static class Passwordhelper {private static bool isupdating = FALSE;
public static readonly DependencyProperty Attachpasswordproperty = dependencyproperty.registerattached ("ATTACHP Assword ", typeof (String), typeof (Passwordhelper), new PropertyMetadata (string. Empty, onattachpasswordpropertychanged)); public static string Getattachpassword (DependencyObject obj) {return (string) obj. GetValue (Attachpasswordproperty); public static void Setattachpassword (DependencyObject obj, string value) {obj. SetValue (attachpasswordproperty, value); }
public static readonly DependencyProperty Attachproperty = dependencyproperty.registerattached ("Attach", typeof (bool), typeof (Passwordhelper), new PropertyMetadata (False, onattachpropertychanged));
public static bool Getattach (DependencyObject obj) {return (bool) obj. GetValue (Attachproperty); public static void Setattach (DependencyObject obj, bool value) {obj. SetValue (attachproperty, value); }
private static void Onattachpropertychanged (DependencyObject sender, DependencyPropertyChangedEventArgs e) { PasswordBox PasswordBox = sender as PasswordBox; if (PasswordBox = = null) return; if (bool) e.oldvalue) passwordbox.passwordchanged-= passwordchanged; if (bool) e.newvalue) passwordbox.passwordchanged + = passwordchanged; }
private static void Onattachpasswordpropertychanged (DependencyObject sender, DependencyPropertyChangedEventArgs e) {if (!isupdating) {PasswordBox PasswordBox = sender as PasswordBox; if (PasswordBox = = null) return;
Passwordbox.password = (string) e.newvalue; } }
private static void PasswordChanged (object sender, RoutedEventArgs e) &NBS P { PasswordBox PasswordBox = sender as passwordbox; &NB Sp isupdating = true; Setattachpassword (PasswordBox, Passwordbox.password); isupdating = false; } }} "" < PasswordBox grid.column= "1" margin= "5" loc:passwordhelper.attach= "True" loc:passwordhelper.attachpassword= "{Binding Text, elementname= Plain, Mode=twoway} "> </passwordbox><textblock name=" plain " grid.row= "1" grid.column= "1" margin= "5" ></textblock><button ...></button> "# The reasons for the non-dependence of risk passwordbox.password I think it's for security reasons, and view and ViewmodIt is inevitable that El can be driven by databinding.
! [Snoop.png] (http://upload-images.jianshu.io/upload_images/140233-fe479aa7a42370e6.png) (see picture does not speak)
Whether this scheme is adopted, the main source of contradiction is security and mvvm~
From for notes (Wiz)
Using the PasswordBox control in MVVM