【转】WPF PasswordBox不支持绑定解决方法
原文地址:https://www.cnblogs.com/wxjing67/p/3935717.html
PasswordBox的Password属性因为安全原因不支持直接绑定,可以使用依赖属性实现。直接插入代码
public class PasswordBoxHelper
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password",
typeof(string), typeof(PasswordBoxHelper),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty AttachProperty =
DependencyProperty.RegisterAttached("Attach",
typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach));
private static readonly DependencyProperty IsUpdatingProperty =
DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
typeof(PasswordBoxHelper)); 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);
}
}
使用:
<PasswordBox helper:PasswordHelper.Attach="True" helper:PasswordHelper.Password="{Binding Model.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<PasswordBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding LoginCommand}"/>
</PasswordBox.InputBindings>
</PasswordBox>
【转】WPF PasswordBox不支持绑定解决方法的更多相关文章
- WPF PasswordBox不支持绑定解决方法
原文:WPF PasswordBox不支持绑定解决方法 PasswordBox的Password属性因为安全原因不支持直接绑定,可以使用依赖属性实现.直接插入代码 public class Passw ...
- max-height,min-height在IE下不支持的解决方法
max-height,min-height在IE下不支持的解决方法 max-width:160px; max-height:160px; _width:expression(this.width &g ...
- 兼容firefox,ie,谷歌,阻止浏览器冒泡事件,Firefox不支持event解决方法
兼容firefox,ie,谷歌,阻止浏览器冒泡事件,Firefox不支持event解决方法 // 获取事件function getEvent(){ if(window.event) {return w ...
- VMware Workstation下安装centos提示硬盘不支持的解决方法
最近在搞centos,遇见的一些问题,记录一下 首先是安装问题 安装过程参考http://www.cnblogs.com/seesea125/archive/2012/02/25/2368255.ht ...
- Firefox不支持event解决方法
IE 中可以直接使用event 对象,而FF 中则不可以,解决方法之一如下: var theEvent = window.event || arguments.callee.caller.argume ...
- jquery on()方法重复绑定解决方法
最近再一次项目中发现 不刷新页面的情况下使用on()方法绑定事件会出现重复执行的问题,意思就是说点击一次会绑定一次...点击n次会绑定n次,执行起来是以你绑定的次数为准,绑定了n次就会执行n次 解决办 ...
- VB6.0对鼠标滚轮不支持的解决方法[转]已经测试work
今天要修改一个老DLL文件,安装了vb6,用起来很不爽. VB6编辑器 和 VBA编辑器 (Office 中的VB编辑器)都不支持鼠标滚动. 但 MS 已经提供了补丁http://download.m ...
- iis8 默认不支持svc解决方法
最近在IIS8中发布WCF服务应用时,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF服务的Managed Handler,按照以下步骤添加后,IIS8即支持WCF服务. 1. 首 ...
- lnmp环境不支持require 解决方法
lnmp环境配置: 3处改动地方---->排查过程: 从phpinfo.php中查找php.ini-----所在位置 /usr/local/php/etc/php.ini 1.display_e ...
随机推荐
- Android为TV端助力 转载自jguangyou的博客,XML基本属性大全
android:layout_width 指定组件布局宽度 android:layout_height 指定组件布局高度 android:alpha 设置组件透明度 android:backgroun ...
- MySQL 约束、表连接、表关联、索引
一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性. 外键:是另一表的唯一性 ...
- 基于cifar10实现卷积神经网络图像识别
import tensorflow as tf import numpy as np import math import time import cifar10 import cifar10_inp ...
- MySQL多表更新的一个坑
简述 MySQL支持update t1,t2 set t1.a=2;这种语法,别的关系数据库例如oracle和sql server都不支持.这种语法有时候写起来挺方便,但他有一个坑. 测试脚本 dro ...
- .gho文件检查
虽然目前windows10的接受程度越来越广泛,但我接触到的一些非IT人士还是钟爱于windows7系统,本文记录一下在使用ghost还原系统遇到的问题. gho还原失败 在还原ghost系统过程中, ...
- irc 关键操作
IRC 客户端: Textual 5 HexChat IRC 用户密码常用命令: 用户密码: 忘记密码 如果太长时间没登录IRC,难免会忘记密码,那IRC有重置密码的功能吗?当然有,不过也是通过命令 ...
- oracle数据库访问形式
1. sql plus访问, sqlplus.exe 2. sql developer访问,sqldeveloper.exe 3. pl/sql 自己下载 4. browse https://loca ...
- phprpc的使用示例以及报错Fatal error: Cannot redeclare gzdecode() in D:\wamp\www\immoc\phprpc\compat.php 处理
今天看书,发现了PHPRPC这个好东东,故在此写下来以作笔记. PHPRPC 是一个轻型的.安全的.跨网际的.跨语言的.跨平台的.跨环境的.跨域的.支持复杂对象传输的.支持引用参数传递的.支持内容输出 ...
- vbs脚本,电脑说出指定内容
新建文件, 输入一下脚本, 然后保存文件为 .vbs 文件, 双击脚本, 电脑就能听到电脑发出 你好 的声音 CreateObject("SAPI.SpVoice").Speak& ...
- ArrayList和LinkedList在中间开始插入的快慢比较
首先创建带数据的两个List, 然后在中间插入数据, 观察完成时间 public class Hero { public static void main(String[] args){ listAn ...