在做公司的项目中,用到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的事了,和本文无关,记录到此。

mvvmlight下passwordBox绑定的解决方法的更多相关文章

  1. max-height,min-height在IE下不支持的解决方法

    max-height,min-height在IE下不支持的解决方法 max-width:160px; max-height:160px; _width:expression(this.width &g ...

  2. android keytool 不是内部命令或外部命令在 (win7下不能用的解决方法)

    android 关于MD5指纹中 keytool在win7下不能用的解决方法 只要在cmd中执行如下命令即可:注意C:\Users\Administrator\.android\debug.keyst ...

  3. CentOS7 下linux不能上网解决方法​,centos7 eth0 没有ip,IP突然丢失

    CentOS7 下linux不能上网解决方法​ 在CentOS VMware下安装好linux后,发现有时不能直接联网,特分享下总结出来的经验,希望对新手有用 工具/原料 XP系统 VMware.Wo ...

  4. IE6-7下margin-bottom不兼容解决方法(非原创,视频中看到的)

    在IE低版本下有很多不兼容,现在将看到的   IE6-7下margin-bottom不兼容解决方法   演示一下,方便日后自己查阅. <!DOCTYPE html> <html la ...

  5. 高DPI下界面错乱的解决方法和原理

    来源: http://bbs.csdn.net/topics/370177760 我在win32 + c写的界面中解决办法,就是把字体的字号给固定了,这样做的结果就是,不管dpi是否有改变,界面中控件 ...

  6. android设计的布局在阿拉伯语下界面错乱的解决方法

    (1)正在AndroidManifest.xml声明文件的application元素中,增加” android:supportsRtl=true” (2)建] androidの设计的布局在阿拉伯语下界 ...

  7. Win8 传统桌面下无法上网的解决方法

    Win8 很酷,就连出现的问题也都酷得不行~ 之前遇到的一些问题与解决方法,避免重新安装 1. Word 2013下, 输入法突然秀逗, 经常按了一个键后, 死循环输入该字符直到死机 解决方法: 进入 ...

  8. hadoop 完全分布式 下 datanode无法启动解决方法

    问题描述: 在集群模式下更改节点后,启动集群发现 datanode一直启动不起来. 我集群配置:有5个节点,分别为master slave1-5 . 在master以Hadoop用户执行:start- ...

  9. nginx 自动忽略request中header name包含下划线参数的解决方法

    使用nginx过程中遇到了个问题,就是request中的header name中如果包含下划线会自动忽略掉,导致服务器接收不到该字段的内容,以下为解决方法: nginx默认request的header ...

随机推荐

  1. JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)

    字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...

  2. canvas绘制线和矩形

    ###canvas绘制矩形 HTML中的元素canvas只支持一种原生的图形绘制:矩形.所有其他的图形的绘制都至少需要生成一条路径 1.绘制矩形 canvas提供了三种方法绘制矩形: ----> ...

  3. python处理多线程之间事件通讯方法

    一.什么是事件 每执行一个事情,肯定有该事情的执行后状态,那事件就是该事情发生的信号 在程序中,多线程之间需要通讯,而事件就是方便线程之间的通讯 案例: 1.服务器启动需要5秒 2.客服端启动后去链接 ...

  4. CNN 常用的几个模型

    LeNet5 论文:http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf LeNet-5:是Yann LeCun在1998年设计的用于手写数字识别的卷 ...

  5. VS2010-MFC(对话框:颜色对话框)

    转自:http://www.jizhuomi.com/software/177.html 颜色对话框大家肯定也不陌生,我们可以打开它选择需要的颜色,简单说,它的作用就是用来选择颜色.MFC中提供了CC ...

  6. 解决ubuntu16.04鼠标键盘自动休眠导致的失灵问题

    sudo vi /etc/laptop-mode/conf.d/runtime-pm.conf 把 AUTOSUSPEND_RUNTIME_DEVTYPE_BLACKLIST="" ...

  7. 求一个n!中尾数有多少个零

    题目描述: 输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2 输入描述: 输入为一行,n(1 ≤ n ≤ 1000) 输出描述: 输出 ...

  8. Android按钮绑定四种方式

    public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCr ...

  9. 第十三篇:一点一滴学ibatis(二)映射文件

       首先给出一个常见的映射文件局部模板 <?xml version="1.0" encoding="utf-8" ?><!DOCTYPE s ...

  10. 20.multi_case07

    # coding:utf-8 import re import ssl import csv import json import time import random import asyncio ...