mvvmlight下passwordBox绑定的解决方法

在做公司的项目中,用到MVVMLight框架,需要做登陆注册的功能模块,但是在wpf中passwordBox没有内置绑定功能,因为出于安全的考虑。但是,博主又非常想要契合mvvm思想,一定要passwordBox在View绑定到ViewModel,经过网上查找,以及多次实验,终于成功。特此记录。

(开发环境:vs2015+EF+mvvmlight+sql server2016)

首先建一个Helper类:

    public class PasswordBindingHelper
    {
        public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.RegisterAttached("Password",
        typeof(string), typeof(PasswordBindingHelper),
        new FrameworkPropertyMetadata(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,
            DependencyPropertyChangedEventArgs 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 passwordBox = 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 passwordBox = sender as PasswordBox;
            SetIsUpdating(passwordBox, true);
            SetPassword(passwordBox, passwordBox.Password);
            SetIsUpdating(passwordBox, false);
        }
    }

View:

<Page x: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>
        <Binding Path="LoginPage" Source="{StaticResource Locator}">Binding>
    Page.DataContext>

    <Grid VerticalAlignment="Top" Width="512" Height="417">
        <Grid.Background>
            <ImageBrush ImageSource="Resources/bg_login_popup.png" />
        Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="60">RowDefinition>
            <RowDefinition Height="58">RowDefinition>
            <RowDefinition Height="20">RowDefinition>
            <RowDefinition Height="58">RowDefinition>
            <RowDefinition Height="25">RowDefinition>
            <RowDefinition Height="29">RowDefinition>
            <RowDefinition Height="60">RowDefinition>
            <RowDefinition Height="57">RowDefinition>
            <RowDefinition Height="52">RowDefinition>
        Grid.RowDefinitions>
        <StackPanel Grid.Row="1">
            <Grid Height="58" Width="404">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="115*">ColumnDefinition>
                    <ColumnDefinition Width="289*">ColumnDefinition>
                Grid.ColumnDefinitions>
                <TextBox  Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource SltTxtLogin}" Text="{Binding LoginUser.UserName,UpdateSourceTrigger=PropertyChanged}"/>
                <Label Grid.Column="0" FontSize="18" Foreground="White" Content="用户名" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            Grid>
        StackPanel>
        <StackPanel Grid.Row="3">
            <Grid Height="58" Width="404">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="115*">ColumnDefinition>
                    <ColumnDefinition Width="289*">ColumnDefinition>
                Grid.ColumnDefinitions>
                <PasswordBox Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource SltPwbLogin}" MaxLength="20" helper:PasswordBindingHelper.Password="{Binding Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                <Label Grid.Column="0" FontSize="18" Foreground="White" Content="密码" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            Grid>
        StackPanel>
        <StackPanel Grid.Row="5">
            <Grid Width="404">
                <CheckBox Style="{StaticResource MyCheckBoxStyle}" HorizontalAlignment="Left" Margin="15,0,0,0" IsChecked="{x:Null}">CheckBox>
            Grid>
        StackPanel>
        <StackPanel Grid.Row="7" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Style="{StaticResource StlBtnLogin}" />
            <Button Style="{StaticResource StlBtnRegister}" Margin="72,0,0,0"/>
        StackPanel>
    Grid>
Page>

剩下的Model和ViewModel以及ViewModelLocator就是MVVMLight的事了,和本文无关,记录到此。

 

转载于:https://www.cnblogs.com/dotnetHui/p/8340072.html

你可能感兴趣的:(mvvmlight下passwordBox绑定的解决方法)