在做公司的项目中,用到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. vue cli3使用webpack4打包优化

    去掉console.log,以及开启gzip const CompressionPlugin = require('compression-webpack-plugin');//引入gzip压缩插件 ...

  2. yum 安装rpmbuild命令

    yum install -y rpm-build [root@zhu2 bin]# rpm -qf rpmbuildrpm-build-4.8.0-27.el6.x86_64

  3. [JZOJ2865]【集训队互测 2012】Attack

    题目 题目大意 平面上有一堆带权值的点.两种操作:交换两个点的权值,查找一个矩形的第\(k\)小 \(N<=60000\) \(M<=10000\) \(10000ms\) 思考历程&am ...

  4. cocos2D-X 常用功能封装

    Packaging_Kernel.h #pragma once #include <string> #include <map> #include <vector> ...

  5. R语言 数据重塑

    R语言数据重塑 R语言中的数据重塑是关于改变数据被组织成行和列的方式. 大多数时间R语言中的数据处理是通过将输入数据作为数据帧来完成的. 很容易从数据帧的行和列中提取数据,但是在某些情况下,我们需要的 ...

  6. git使用过程中问题

    git提交文件命令顺序 fetch merge add commit pull push 取消已add文件 git reset HEAD 文件名 覆盖本地文件 git checkout 文件名 $ g ...

  7. 阿里云在云栖大会发布RPA最新3.4版本,将与达摩院联合探索人工智能领域

    9月26日,在2019年杭州云栖大会上,阿里云发布了RPA最新V3.4版本,全新升级了增加诸如录屏审计.JAVA应用录制能力.达摩院OCR内置组件.语法检查与智能提示能力增强等功能. RPA全名称Ro ...

  8. goconvey测试模块

    一.介绍 是一款针对Golang的测试框架,可以管理和运行测试用例,同时提供了丰富的断言函数,并支持很多 Web 界面特性. GoConvey 网站 : http://smartystreets.gi ...

  9. VS2010-MFC(常用控件:列表视图控件List Control 上)

    转自:http://www.jizhuomi.com/software/195.html      列表视图控件简介 列表视图控件List Control同样比较常见,它能够把任何字符串内容以列表的方 ...

  10. 2017/8/4 SCJP学习

    2 Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . 85 Encapsulation (Exam Object ...