Due to PasswordBox. The Password property is not a dependency property, so it cannot be the target of the binding, the following is my MVVM implementation method.
Passwordbox.password is synchronized with TextBox.Text, and the textbox is only for demonstration purposes, actually using the textbox.text binding source.
By behavior adding an event handler for the PasswordChanged event, and customizing an attached property implementation binding, behavior needs to refer to System.Windows.Interactivity.dll, the code is as follows:
usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Interactivity;namespacepasswordhelpertest{ Public Static classPasswordboxhelper { Public Static ReadOnlyDependencyProperty Passwordproperty =dependencyproperty.registerattached ("Password", typeof(string),typeof(passwordboxhelper),NewFrameworkpropertymetadata (string. Empty, onpasswordpropertychanged)); Private Static voidonpasswordpropertychanged (DependencyObject sender, DependencyPropertyChangedEventArgs e) {Password Box PasswordBox= Sender asPasswordBox; stringPassword = (string) E.newvalue; if(PasswordBox! =NULL&& Passwordbox.password! =password) {Passwordbox.password=password; } } Public Static stringGetPassword (DependencyObject dp) {return(string) DP. GetValue (Passwordproperty); } Public Static voidSetPassword (DependencyObject DP,stringvalue) {DP. SetValue (passwordproperty, value); } } Public classPasswordboxbehavior:behavior<passwordbox> { protected Override voidonattached () {Base. Onattached (); Associatedobject.passwordchanged+=onpasswordchanged; } Private Static voidOnpasswordchanged (Objectsender, RoutedEventArgs e) {PasswordBox PasswordBox= Sender asPasswordBox; stringPassword =Passwordboxhelper.getpassword (PasswordBox); if(PasswordBox! =NULL&& Passwordbox.password! =password) {Passwordboxhelper.setpassword (PasswordBox, Passwordbox.password); } } protected Override voidondetaching () {Base. Ondetaching (); Associatedobject.passwordchanged-=onpasswordchanged; } }}
The view code is as follows:
<window x:class="Passwordhelpertest.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"xmlns:local="clr-namespace:passwordhelpertest"Title="MainWindow"height=" -"Width="525"> <StackPanel> <passwordbox margin="3"local:passwordboxhelper.password="{Binding path=password,mode=twoway,updatesourcetrigger=propertychanged}"> <i:interaction. Behaviors> <local:passwordboxbehavior/> </i:interaction. behaviors> </PasswordBox> <textbox margin="3"text="{Binding path=password,mode=twoway,updatesourcetrigger=propertychanged}"></TextBox> </StackPanel></Window>
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespacepasswordhelpertest{/// <summary> ///the interactive logic of MainWindow.xaml/// </summary> Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); This. DataContext =NewMainwindowviewmodel (); } }}
ViewModel reference Microsoft.Practices.Prism.dll, the code is as follows:
usingMicrosoft.Practices.Prism.ViewModel;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacepasswordhelpertest{classMainwindowviewmodel:notificationobject {Private string_password; Public stringPassword {Get{return_password;} Set { if(_password! =value) {_password=value; This. raisePropertyChanged ("Password"); } } } }}
WPF PasswordBox MVVM Implementation