在做公司的项目中,用到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事件 什么是事件?JavaScript 创建动态页面。事件是可以被 JavaScript 侦测到的行为。 网页中的每个元素都可以产生某些可以触发 JavaScript 函数或程序的事件。

    什么是事件 JavaScript 创建动态页面.事件是可以被 JavaScript 侦测到的行为. 网页中的每个元素都可以产生某些可以触发 JavaScript 函数或程序的事件. 比如说,当用户单击 ...

  2. 【学术篇】SDOI2008 沙拉公主的困惑

    传送门! 题目在这里... 题目大意? 难道不是说的很清楚了么OvO 求n!中与m!互质的数的个数.. 题目分析. 显然的数论... 所以就是化式子呗.. 一个很显然的性质就是如果\(gcd(a,b) ...

  3. vc面试题目

    class B { public: B() { cout << "default constructor" << endl; } ~B() { cout & ...

  4. javaScript 习题总结(持续更新)

    判定偶数 function collect_all_even(collection) { return collection.filter(item => item%2 == 0); } 两个集 ...

  5. spring整合shiro框架

    上一篇文章已经对shiro框架做了一定的介绍,这篇文章讲述使用spring整合shiro框架,实现用户认证已经权限控制 1.搭建环境 这里不在赘述spring环境的搭建,可以简单的搭建一个ssm框架, ...

  6. sprintf、fprintf和printf这三个函数有什么区别?

    都是把格式好的字符串输出,只是输出的目标不一样:1 printf,是把格式字符串输出到标准输出(一般是屏幕,可以重定向).2 sprintf,是把格式字符串输出到指定字符串中,所以参数比printf多 ...

  7. Kafka和RabbitMQ 对比

    1)  Kafka成为业界大数据松耦合架构,异步,队列 特点:吞吐量高50m/s. Kafka和RabbitMQ都是MQ机制,它差异 Kafka作为大数据产品,可以作为数据源,也可以作为结果数据中转 ...

  8. JSF(JavaServer Faces)简介

    JavaServer Faces (JSF) 是一种用于构建Java Web 应用程序的标准框架(是Java Community Process 规定的JSR-127标准).它提供了一种以组件为中心的 ...

  9. spss logistic回归分析结果如何分析

    spss logistic回归分析结果如何分析 如何用spss17.0进行二元和多元logistic回归分析 一.二元logistic回归分析 二元logistic回归分析的前提为因变量是可以转化为0 ...

  10. jquery判断是pc端还是移动端

    原文地址:https://www.cnblogs.com/mo-cha/p/6038872.html $(function(){ var system = { win: false, mac: fal ...