Workarounds for PasswordBox bindings under Mvvmlight

Source: Internet
Author: User

In the company's project, using the Mvvmlight framework, you need to do a login registration function module, but in WPF PasswordBox has no built-in binding functionality, because of security considerations. However, bloggers also very want to fit the MVVM idea, must passwordbox in the view binding to the ViewModel, after the online search, as well as many experiments, finally succeeds. Hereby record.

(Development environment: Vs2015+ef+mvvmlight+sql server2016)

First build a helper class:

    public class Passwordbindinghelper {public static readonly DependencyProperty Passwordproperty = Dep Endencyproperty.registerattached ("Password", typeof (String), typeof (Passwordbindinghelper), new Frameworkpro Pertymetadata (String.        Empty, onpasswordpropertychanged));            public static readonly DependencyProperty Attachproperty = dependencyproperty.registerattached ("Attach",        typeof (BOOL), typeof (Passwordbindinghelper), new PropertyMetadata (False, Attach)); private static readonly DependencyProperty Isupdatingproperty = dependencyproperty.registerattached ("isupdating"        , typeof (BOOL), typeof (Passwordbindinghelper)); public static void Setattach (DependencyObject DP, BOOL value) {DP.        SetValue (attachproperty, value); public static bool Getattach (DependencyObject DP) {return (BOOL) DP.        GetValue (Attachproperty); } public static string GETPAssWOrd (DependencyObject dp) {return (string) DP.        GetValue (Passwordproperty); public static void SetPassword (DependencyObject dp, String value) {DP.        SetValue (passwordproperty, value); } private static bool Getisupdating (DependencyObject DP) {return (BOOL) DP.        GetValue (Isupdatingproperty); } private static void Setisupdating (DependencyObject DP, BOOL value) {DP.        SetValue (isupdatingproperty, value); } private static void Onpasswordpropertychanged (DependencyObject sender, Dependencypropertychangedeventa            RGS e) {PasswordBox PasswordBox = sender as PasswordBox;            Passwordbox.passwordchanged-= passwordchanged; if (! (            BOOL) getisupdating (PasswordBox)) {Passwordbox.password = (string) e.newvalue;        } passwordbox.passwordchanged + = passwordchanged; } private Static void Attach (DependencyObject sender, DependencyPropertyChangedEventArgs e) {PasswordBox PA            Sswordbox = sender as PasswordBox;            if (PasswordBox = = null) return;            if (bool) e.oldvalue) {passwordbox.passwordchanged-= passwordchanged;            } if ((bool) e.newvalue) {passwordbox.passwordchanged + = passwordchanged; }} private static void PasswordChanged (object sender, RoutedEventArgs e) {PasswordBox P            Asswordbox = sender as PasswordBox;            Setisupdating (PasswordBox, true);            SetPassword (PasswordBox, Passwordbox.password);        Setisupdating (PasswordBox, false); }    }

View:

<Pagex:class= "Sggs." SmartGroundGuidence.Pages.LoginPage "xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"XMLNS:MC= "http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"xmlns:local= "Clr-namespace:sggs." Smartgroundguidence.pages "Xmlns:helper= "Clr-namespace:sggs.smartgroundguidence.commonhelper"mc:ignorable= "D"Title= "LoginPage">    <Page.datacontext>        <BindingPath= "LoginPage"Source="{StaticResource Locator}"></Binding>    </Page.datacontext>    <GridVerticalAlignment= "Top"Width= "+"Height= "417">        <Grid.background>            <ImageBrushImageSource= "Resources/bg_login_popup.png" />        </Grid.background>        <grid.rowdefinitions>            <RowDefinitionHeight= "$"></RowDefinition>            <RowDefinitionHeight= "+"></RowDefinition>            <RowDefinitionHeight= " the"></RowDefinition>            <RowDefinitionHeight= "+"></RowDefinition>            <RowDefinitionHeight= "+"></RowDefinition>            <RowDefinitionHeight= "$"></RowDefinition>            <RowDefinitionHeight= "$"></RowDefinition>            <RowDefinitionHeight= "The "></RowDefinition>            <RowDefinitionHeight= "The "></RowDefinition>        </grid.rowdefinitions>        <StackPanelGrid.Row= "1">            <GridHeight= "+"Width= "404">                <grid.columndefinitions>                    <ColumnDefinitionWidth= "115*"></ColumnDefinition>                    <ColumnDefinitionWidth= "289*"></ColumnDefinition>                </grid.columndefinitions>                <TextBoxGrid.column= "0"Grid.columnspan= "2"Style="{StaticResource Slttxtlogin}"Text="{Binding loginuser.username,updatesourcetrigger=propertychanged}"/>                <LabelGrid.column= "0"FontSize= "+"Foreground= "White"Content= "User name"VerticalAlignment= "Center"HorizontalAlignment= "Center"/>            </Grid>        </StackPanel>        <StackPanelGrid.Row= "3">            <GridHeight= "+"Width= "404">                <grid.columndefinitions>                    <ColumnDefinitionWidth= "115*"></ColumnDefinition>                    <ColumnDefinitionWidth= "289*"></ColumnDefinition>                </grid.columndefinitions>                <PasswordBoxGrid.column= "0"Grid.columnspan= "2"Style="{StaticResource Sltpwblogin}"MaxLength= " the"Helper:PasswordBindingHelper.Password="{Binding password,mode=twoway,updatesourcetrigger=propertychanged}"/>                <LabelGrid.column= "0"FontSize= "+"Foreground= "White"Content= "Password"VerticalAlignment= "Center"HorizontalAlignment= "Center"/>            </Grid>        </StackPanel>        <StackPanelGrid.Row= "5">            <GridWidth= "404">                <CheckBoxStyle="{StaticResource Mycheckboxstyle}"HorizontalAlignment= "Left"Margin= "15,0,0,0"IsChecked="{x:null}"></CheckBox>            </Grid>        </StackPanel>        <StackPanelGrid.Row= "7"Orientation= "Horizontal"HorizontalAlignment= "Center">            <ButtonStyle="{StaticResource Stlbtnlogin}" />            <ButtonStyle="{StaticResource Stlbtnregister}"Margin= "72,0,0,0"/>        </StackPanel>    </Grid></Page>

The rest of the model and ViewModel and Viewmodellocator are mvvmlight, not related to this article, and are recorded here.

Workarounds for PasswordBox bindings under Mvvmlight

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.