当尝试数据绑定一个PasswordBox的密码属性,你会认识到,你不能做数据就可以了绑定。 这样做的原因是,该密码属性不是由DependencyProperty支持。
原因是数据绑定密码是不是一个好的设计,出于安全考虑,应该避免。 但有时这种安全是没有必要的,那么它不但麻烦,你不能绑定到Password属性。 在这种特殊情况下,你可以利用以下PasswordBoxBindingHelper的。
<passwordbox name="pwdboxpwd"tools:PasswordBoxBindingHelper.IsPasswordBindingEnabled="True"Tools:PasswordBoxBindingHelper.BindedPassword="{Binding Path=password, Mode=twoway, updatesourcetrigger=propertychanged}"HorizontalAlignment=" Left"Height=" -"margin="167,170,0,0"Passwordchar="*"Verticalalignment="Top"Width="258"Fontsize=" -"Verticalcontentalignment="Center"Maxlength=" -"/>
Passwordboxbindinghelper is attached to PasswordBox by calling Passwordboxbindinghelper.bindedpasswordproperty. The attached property Passwordboxbindinghelper.password provides a copy of the PasswordBox control's original password property password for binding.
1 Public Static classPasswordboxbindinghelper2 {3 //Bindedpassword4 Public Static ReadOnlyDependencyProperty Bindedpasswordproperty =5Dependencyproperty.registerattached ("Bindedpassword",6 typeof(string),typeof(passwordboxbindinghelper),7 NewFrameworkpropertymetadata (string. Empty, onbindedpasswordchanged));8 9 Public Static stringGetbindedpassword (DependencyObject dp)Ten { One return(string) DP. GetValue (bindedpasswordproperty); A } - Public Static voidSetbindedpassword (DependencyObject DP,stringvalue) - { the DP. SetValue (bindedpasswordproperty, value); - } - - Private Static voidonbindedpasswordchanged (DependencyObject sender, + DependencyPropertyChangedEventArgs e) - { +PasswordBox PasswordBox = Sender asPasswordBox; A if(PasswordBox = =NULL) at { - return; - } -Passwordbox.passwordchanged-=passwordchanged; - - if(! (BOOL) getisupdating (PasswordBox)) in { -Passwordbox.password = (string) E.newvalue; to } +Passwordbox.passwordchanged + =passwordchanged; - } the * $ //ispasswordbindingenabledPanax Notoginseng Public Static ReadOnlyDependencyProperty Ispasswordbindingenabledproperty = -Dependencyproperty.registerattached ("ispasswordbindingenabled", the typeof(BOOL),typeof(Passwordboxbindinghelper),NewPropertyMetadata (false, ispasswordbindingenabled)); + Public Static voidSetispasswordbindingenabled (DependencyObject DP,BOOLvalue) A { the DP. SetValue (ispasswordbindingenabledproperty, value); + } - $ Public Static BOOLgetispasswordbindingenabled (DependencyObject dp) $ { - return(BOOL) DP. GetValue (ispasswordbindingenabledproperty); - } the Private Static voidispasswordbindingenabled (DependencyObject sender, - DependencyPropertyChangedEventArgs e)Wuyi { thePasswordBox PasswordBox = Sender asPasswordBox; - Wu if(PasswordBox = =NULL) - return; About $ if((BOOL) e.oldvalue) - { -Passwordbox.passwordchanged-=passwordchanged; - } A + if((BOOL) e.newvalue) the { -Passwordbox.passwordchanged + =passwordchanged; $ } the } the the //isupdating the Private Static ReadOnlyDependencyProperty Isupdatingproperty = -Dependencyproperty.registerattached ("isupdating",typeof(BOOL), in typeof(Passwordboxbindinghelper)); the the Private Static BOOLgetisupdating (DependencyObject dp) About { the return(BOOL) DP. GetValue (isupdatingproperty); the } the + Private Static voidSetisupdating (DependencyObject DP,BOOLvalue) - { the DP. SetValue (isupdatingproperty, value);Bayi } the the Private Static voidPasswordChanged (Objectsender, RoutedEventArgs e) - { -PasswordBox PasswordBox = Sender asPasswordBox; theSetisupdating (PasswordBox,true); the Setbindedpassword (PasswordBox, Passwordbox.password); theSetisupdating (PasswordBox,false); the } - } the}
WPF issues with PasswordBox binding password (MVVM framework)