日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WPF密碼如何綁定在密碼框中

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

創(chuàng)新互聯(lián)專業(yè)提供綿陽主機托管服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購買綿陽主機托管服務(wù),并享受7*24小時金牌售后服務(wù)。

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

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

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

 
 
 
  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密碼后, 需要手動更新密碼框插入符(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 });  

網(wǎng)站標(biāo)題:WPF密碼如何綁定在密碼框中
新聞來源:http://www.5511xx.com/article/coighcj.html