自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WPF密碼如何綁定在密碼框中

開發(fā) 開發(fā)工具
WPF密碼作為WPF中的一個(gè)password屬性,可以對(duì)其進(jìn)行綁定。這在WVVM模式中是一個(gè)非常必要的操作。并且在安全性方面也是很重要的。

WPF開發(fā)工具在實(shí)際使用中有很多功能的實(shí)現(xiàn),需要我們?nèi)ド钊氲奶接憽1热邕@篇文章為大家介紹有關(guān)WPF密碼的一些應(yīng)用方法等。#t#

正如綁定TextBox控件的Text屬性一樣, 我們希望能夠?qū)asswordBox空間的WPF密碼Password屬性進(jìn)行綁定, 比如在MVVM模式中,這似乎是必須的, 但可惜的是, Password屬性是不支持綁定的(不是依賴屬性, 也沒有實(shí)現(xiàn)INotifyPropertyChanged).

這可能是出于安全性的考慮. 但在我們的系統(tǒng)為了實(shí)現(xiàn)View層密碼框中的密碼與后臺(tái)其它層之間的密碼屬性之間的綁定, 可以采取如下思路: 將密碼框的密碼和某一個(gè)緩沖區(qū)進(jìn)行同步, 緩沖區(qū)在和后臺(tái)進(jìn)行綁定. 其中密碼框與緩沖區(qū)之間的同步可采用事件進(jìn)行通知, 并將緩沖區(qū)打造成依賴屬性, 然后緩沖區(qū)就支持綁定了, 并給后臺(tái)提供正確的密碼.

緩沖區(qū)可以是哈希表或其他字典結(jié)構(gòu), 以便將WPF密碼框和緩沖區(qū)中的密碼一 一對(duì)應(yīng)起來, 也可以使AttachProperty(附加屬性), 其實(shí)附加屬性的機(jī)制也就是對(duì)緩存了的一個(gè)大字典進(jìn)行操作

 

  1. public static class Password
    BoxBindingHelper  
  2. {  
  3. public static bool GetIsPassword
    BindingEnabled(DependencyObject obj)  
  4. {  
  5. return (bool)obj.GetValue
    (IsPasswordBindingEnabledProperty);  
  6. }  
  7. public static void SetIsPassword
    BindingEnabled(DependencyObject 
    obj, bool value)  
  8. {  
  9. obj.SetValue(IsPasswordBinding
    EnabledProperty, value);  
  10. }  
  11. public static readonly Dependency
    Property 
    IsPasswordBinding
    EnabledProperty
     =  
  12. DependencyProperty.Register
    Attached("IsPasswordBinding
    Enabled", typeof(bool),   
  13. typeof(PasswordBoxBindingHelper),   
  14. new UIPropertyMetadata
    (false, OnIsPasswordBinding
    EnabledChanged));  
  15. private static void 
    OnIsPasswordBindingEnabled
    Changed(DependencyObject obj,   
  16. DependencyPropertyChangedEventArgs e)  
  17. {  
  18. var passwordBox = obj as 
    PasswordBox;  
  19. if(passwordBox != null)  
  20. {  
  21. passwordBox.PasswordChanged 
    -PasswordBoxPasswordChanged;  
  22. if ((bool)e.NewValue)  
  23. {  
  24. passwordBox.PasswordChanged 
    += PasswordBoxPasswordChanged;  
  25. }  
  26. }  
  27. }  
  28. //when the passwordBox's password 
    changed, update the buffer  
  29. static void PasswordBoxPassword
    Changed(object sender, RoutedEventArgs e)  
  30. {  
  31. var passwordBox = (PasswordBox) sender;  
  32. if (!String.Equals(GetBindedPassword
    (passwordBox),passwordBox.Password))  
  33. {  
  34. SetBindedPassword(passwordBox, 
    passwordBox.Password);  
  35. }  
  36. }  
  37. public static string GetBindedPassword
    (DependencyObject obj)  
  38. {  
  39. return (string)obj.GetValue
    (BindedPasswordProperty);  
  40. }  
  41. public static void SetBindedPassword
    (DependencyObject obj, string value)  
  42. {  
  43. obj.SetValue(BindedPasswordProperty, value);  
  44. }  
  45. public static readonly Dependency
    Property 
    BindedPasswordProperty =  
  46. DependencyProperty.RegisterAttached
    ("BindedPassword", typeof(string),   
  47. typeof(PasswordBoxBindingHelper),   
  48. new UIPropertyMetadata(string.Empty, 
    OnBindedPasswordChanged));  
  49. //when the buffer changed, upate 
    the passwordBox's password  
  50. private static void OnBindedPassword
    Changed(DependencyObject obj,   
  51. DependencyPropertyChangedEventArgs e)  
  52. {  
  53. var passwordBox = obj as PasswordBox;  
  54. if (passwordBox != null)  
  55. {  
  56. passwordBox.Password = e.NewValue == 
    null ? string.Empty : e.NewValue.ToString();  
  57. }  
  58. }   

在View層, 如下使用便可以了:

  1. < PasswordBox Helpers:PasswordBox
    BindingHelper.IsPassword
    BindingEnabled
    ="True"   
  2. Helpers:PasswordBoxBinding
    Helper.BindedPassword
    =  
  3. "{Binding Path=Password, 
    Mode=TwoWay, UpdateSource
    Trigger=PropertyChanged}"
     /> 

另外, 在更改了密碼框的WPF密碼后, 需要手動(dòng)更新密碼框插入符(CaretIndex)的位置, 可惜的是, 密碼框并沒有給我們提供這樣的屬性或方法(TextBox有, PasswordBox沒有), 可以采用下面的方法來設(shè)置:

  1. private static void SetPassword
    BoxSelection(PasswordBox 
    passwordBox, int start, int length)  
  2. {  
  3. var select = passwordBox.
    GetType().GetMethod("Select",   
  4. BindingFlags.Instance | 
    BindingFlags.NonPublic);  
  5.  
  6. select.Invoke(passwordBox, 
    new object[] { start, length });  

 

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-12-28 09:50:08

WPF數(shù)據(jù)綁定

2023-10-07 11:04:58

WPF數(shù)據(jù)UI

2024-06-13 15:43:04

2010-01-30 11:23:59

2022-09-15 00:08:46

密碼安全身份驗(yàn)證

2022-05-26 10:30:48

Fedora操作系統(tǒng)root 密碼

2011-03-30 09:13:13

靜態(tài)類Windows Pho

2013-07-05 10:04:47

2012-07-26 10:29:55

Linux操作系統(tǒng)

2022-07-21 09:31:58

Actuator密碼框架

2020-08-31 07:30:28

UbuntuRoot密碼

2018-12-27 13:35:11

MySQLMySQL 8重置密碼

2013-10-08 10:07:58

2021-01-03 09:33:48

密碼數(shù)字身份加密解密

2009-12-24 11:15:59

WPF數(shù)據(jù)綁定

2021-02-11 08:27:28

數(shù)據(jù)

2020-04-14 11:48:59

密碼網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全

2013-07-17 17:15:57

2019-09-09 10:25:54

MySQLMariaDB roo密碼

2009-02-18 20:31:51

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)