WPF PasswordBox不支持绑定解决方法

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>

 

转载于:https://www.cnblogs.com/wxjing67/p/3935717.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/375665.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

error: expected unqualified-id before 'public'

Error Coding class A{ }&#xff1b;class B public : A { };Correct Coding class A{ };class B : public A{ };

C# 方法返回值的个数

方法返回值类型总的来说分为值类型&#xff0c;引用类型,Void 有些方法显示的标出返回值 public int Add(int a,int b) {return ab; } 有些方法隐式的返回返回值&#xff0c;我们可以将上面的方法改改&#xff1a; public void Add(int a,int b,out int sum) {sumab; } 怎么样&…

【Java】Java里String 的equals和==

Java里面有对象和对象的引用的概念&#xff0c;在String方面&#xff0c;比较的是引用&#xff0c;equals比较的是对象的具体值。 String s1 new String("abc");String s2 new String("abc");System.out.println(s1 s2);System.out.println(s1.equals(s…

marked override, but does not override

检查这个函数对应的基类函数 看是否是虚函数看函数参数是否对应

STL中的lower_bound和upper_bound的理解

STL迭代器表述范围的时候&#xff0c;习惯用[a, b)&#xff0c;所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置。 譬如&#xff0c;值val在容器内的时候&#xff0c;从lower_bound到 upper_bound表示的就是整个容器中与val相…

because the following virtual functions are pure within

构建了包含纯虚函数的对象 包含纯虚函数的类不能实例化为对象

C ~ 链式队列与循环队列

此处的链式与循环队列可以应用于BFS和树的层序遍历。下面是对其结构和基本操作的程序描述。 1、循环队列 解决循环队列的队空和队满的方法&#xff1a; [1].增加一个参数count&#xff0c;用来记录数组中当前元素的个数&#xff1b; [2].为避免队空和满两状态混淆&#xff0c;少…

Hexo之部署github

最近开始学NodeJs&#xff0c;准备也在github上弄个一个Hexo博客练练过程中遇到一些问题总结一下。希望对遇到同样问题的同学能有个帮助少走一些弯路。 - 其实用windows或mac客户端直接去同步很顺利没遇到什么问题。主要是在使用Hexo deploy命令的时候。 我的配置文件deploy部分…

You have unstaged changes.

执行git rebase出错 原因&#xff1a;有未提交的修改 解决&#xff1a;执行git stash暂时保存修改&#xff0c;rebase后&#xff0c;执行git stash pop

[bootstrap] 打造一个简单的系统模板(1) 左侧折叠菜单

1. 前言 最近需要做一个后台管理系统&#xff0c;我打算使用bootstrap弄一个好看的后台模板。网上的好多模板我觉的css和js有点重。 于是就打算完全依靠bootstrap搭建一个属于自己的模板。 首先从左侧的折叠菜单开始。看图。 2. CSS 代码 以下是自定义的css代码&#xff0c;由于…

将gcc/g++链接到指定版本

安装指定版本&#xff1a; sudo apt-get install gcc-4.8 sudo apt-get install g-4.8以上命令默认安装的为4.8.5版本&#xff0c;支持c11 建立软连接 cd /usr/bin如果已经装有gcc或者g&#xff0c;需要先移除原先的软连接sudo rm gcc sudo rm g建立新的软连接 sudo ln -s g…

23种设计模式的优点与缺点概况

设计模式 标签&#xff08;空格分隔&#xff09;&#xff1a; 设计模式优点 应用场景 整理自《设计模式之禅》 单例模式 优点&#xff1a; 只有一个实例&#xff0c;减少了内存开支&#xff1b;可以避免对系统资源的多重占用&#xff1b;可以在系统中设置全局的访问点&#xff…

How Many Shortest Path

zoj2760:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode2760 题意&#xff1a;给你一张有向带权图&#xff0c;然后问你最短路径有多少条。 题解&#xff1a;这一题用到了网络流&#xff0c;一开始&#xff0c;我想到用找到一条最短路&#xff0c;然后删除这条…

pat00-自测5. Shuffling Machine (20)

00-自测5. Shuffling Machine (20) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, YueShuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid …

E488: Trailing characters:

情景&#xff1a; 对vim进行配置&#xff0c;配置完成后进行保存&#xff0c;配置完成后打开其他文件时报错。原因&#xff1a; vim 配置文件中保存不合乎语法的语句&#xff0c;报错时如下&#xff1a; #显示行号 set number#字符导致的错误&#xff0c;改成"即可。 vi…

移动web开发总结

1、-webkit-tap-highlight-color:rgba(255,255,255,0)可以同时屏蔽ios和android下点击元素时出现的阴影。 备注&#xff1a;transparent的属性值在android下无效。2、-webkit-appearance:none可以同时屏蔽输入框怪异的内阴影。3,/*去除android浏览器下a/input等元素获得焦点时高…

人物角色群体攻击判定二(叉乘来判断敌人的位置)

建议阅读: 判断敌人在玩家的某一个区域: http://www.cnblogs.com/plateFace/p/4716799.html 我们可以根据玩家和敌人的坐标, 进行叉乘来获取一个向量可以用它来判断敌人的位置, 敌人是否在攻击范围内. 下面我简单实现下对单体敌人是否攻击做判定 这种方式有一种重大的BUG, 假设…

更改linux子系统软件源为国内镜像

cd /etc/apt/sudo cp sources.list sources.list.back20190831sudo vim sources.list执行vim替换命令 :%s/archive.ubuntu/mirrors.aliyun/g:%s/security.ubuntu/mirrors.aliyun/g执行sudo apt update即可。

[Z] Linux下进程的文件访问权限

原文链接&#xff1a;http://blog.csdn.net/chosen0ne/article/details/10581883对进程校验文件访问权限包括两个部分&#xff0c;一是确定进程的角色&#xff08;属于哪个用户或者组&#xff09;&#xff0c;二是确定对应的角色是否具有该操作的权限。 首先看第一部分。默认情…

HDU 5371 Manacher Hotaru's problem

求出一个连续子序列&#xff0c;这个子序列由三部分ABC构成&#xff0c;其中AB是回文串&#xff0c;A和C相同&#xff0c;也就是BC也是回文串。 求这样一个最长的子序列。 Manacher算法是在所有两个相邻数字之间插入一个特殊的数字&#xff0c;比如-1&#xff0c; Manacher算法…