日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

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

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

正如綁定TextBox控件的Text屬性一樣, 我們希望能夠?qū)asswordBox空間的WPF密碼Password屬性進(jìn)行綁定, 比如在MVVM模式中,這似乎是必須的, 但可惜的是, Password屬性是不支持綁定的(不是依賴(lài)屬性, 也沒(méi)有實(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ū)打造成依賴(lài)屬性, 然后緩沖區(qū)就支持綁定了, 并給后臺(tái)提供正確的密碼.

緩沖區(qū)可以是哈希表或其他字典結(jié)構(gòu), 以便將WPF密碼框和緩沖區(qū)中的密碼一 一對(duì)應(yīng)起來(lái), 也可以使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)的位置, 可惜的是, 密碼框并沒(méi)有給我們提供這樣的屬性或方法(TextBox有, PasswordBox沒(méi)有), 可以采用下面的方法來(lái)設(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 });  

當(dāng)前標(biāo)題:WPF密碼如何綁定在密碼框中
網(wǎng)站地址:http://www.dlmjj.cn/article/coighcj.html