WPF PasswordBox MVVM Implementation

Source: Internet
Author: User

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

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.