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.pswp.cn/news/375665.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375665.shtml
英文地址,請注明出處:http://en.pswp.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算法…