The Password property of the WPF PasswordBox control is not a dependency property, it cannot be directly data-bound, and to make it work properly in MVVM mode, you can add an helper class to PasswordBox as follows:
Original link: http://blog.csdn.net/ryb666666/article/details/7629767
1. Create a new Passwordboxhelper helper class
1 Public Static classPasswordboxhelper2 {3 Public Static ReadOnlyDependencyProperty Passwordproperty =4Dependencyproperty.registerattached ("Password",5 typeof(string),typeof(passwordboxhelper),6 NewFrameworkpropertymetadata (string. Empty, onpasswordpropertychanged));7 Public Static ReadOnlyDependencyProperty Attachproperty =8Dependencyproperty.registerattached ("Attach",9 typeof(BOOL),typeof(Passwordboxhelper),NewPropertyMetadata (false, Attach));Ten Private Static ReadOnlyDependencyProperty Isupdatingproperty = OneDependencyproperty.registerattached ("isupdating",typeof(BOOL), A typeof(Passwordboxhelper)); - - Public Static voidSetattach (DependencyObject DP,BOOLvalue) the { - DP. SetValue (attachproperty, value); - } - Public Static BOOLGetattach (DependencyObject dp) + { - return(BOOL) DP. GetValue (attachproperty); + } A Public Static stringGetPassword (DependencyObject dp) at { - return(string) DP. GetValue (passwordproperty); - } - Public Static voidSetPassword (DependencyObject DP,stringvalue) - { - DP. SetValue (passwordproperty, value); in } - Private Static BOOLgetisupdating (DependencyObject dp) to { + return(BOOL) DP. GetValue (isupdatingproperty); - } the Private Static voidSetisupdating (DependencyObject DP,BOOLvalue) * { $ DP. SetValue (isupdatingproperty, value);Panax Notoginseng } - Private Static voidonpasswordpropertychanged (DependencyObject sender, the DependencyPropertyChangedEventArgs e) + { APasswordBox PasswordBox = Sender asPasswordBox; thePasswordbox.passwordchanged-=passwordchanged; + if(! (BOOL) getisupdating (PasswordBox)) - { $Passwordbox.password = (string) E.newvalue; $ } -Passwordbox.passwordchanged + =passwordchanged; - } the Private Static voidAttach (DependencyObject sender, - DependencyPropertyChangedEventArgs e)Wuyi { thePasswordBox PasswordBox = Sender asPasswordBox; - if(PasswordBox = =NULL) Wu return; - if((BOOL) e.oldvalue) About { $Passwordbox.passwordchanged-=passwordchanged; - } - if((BOOL) e.newvalue) - { APasswordbox.passwordchanged + =passwordchanged; + } the } - Private Static voidPasswordChanged (Objectsender, RoutedEventArgs e) $ { thePasswordBox PasswordBox = Sender asPasswordBox; theSetisupdating (PasswordBox,true); the SetPassword (PasswordBox, Passwordbox.password); theSetisupdating (PasswordBox,false); - } in}
View Code
2. xaml binding in the view layer
1 <passwordbox x:name="PasswordBox" passwordchar="*" helper:passwordboxhelper.attach="True"2 Helper: passwordboxhelper.password="{Binding path=pwd,mode=twoway,updatesourcetrigger=propertychanged} "/>
1<window x:class="HR_Test01.View.LoginView"2xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"4 xmlns:helper="clr-namespace:hr_test01.model" 5Xmlns:d="http://schemas.microsoft.com/expression/blend/2008"6Xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"7Xmlns:local="Clr-namespace:hr_test01.view"8mc:ignorable="D"allowstransparency="True"windowstyle="None"mouseleftbuttondown="Window_mouseleftbuttondown" 9title="Login Interface"height=" -"Width=" -"windowstartuplocation="Centerscreen">
WPF PasswordBox Password Data binding