WPF密码如何绑定在密码框中

开发 开发工具
WPF密码作为WPF中的一个password属性,可以对其进行绑定。这在WVVM模式中是一个非常必要的操作。并且在安全性方面也是很重要的。

WPF开发工具在实际使用中有很多功能的实现,需要我们去深入的探讨。比如这篇文章为大家介绍有关WPF密码的一些应用方法等。#t#

正如绑定TextBox控件的Text属性一样, 我们希望能够将PasswordBox空间的WPF密码Password属性进行绑定, 比如在MVVM模式中,这似乎是必须的, 但可惜的是, Password属性是不支持绑定的(不是依赖属性, 也没有实现INotifyPropertyChanged).

这可能是出于安全性的考虑. 但在我们的系统为了实现View层密码框中的密码与后台其它层之间的密码属性之间的绑定, 可以采取如下思路: 将密码框的密码和某一个缓冲区进行同步, 缓冲区在和后台进行绑定. 其中密码框与缓冲区之间的同步可采用事件进行通知, 并将缓冲区打造成依赖属性, 然后缓冲区就支持绑定了, 并给后台提供正确的密码.

缓冲区可以是哈希表或其他字典结构, 以便将WPF密码框和缓冲区中的密码一 一对应起来, 也可以使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没有), 可以采用下面的方法来设置:

  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 });  

 

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-28 09:50:08

WPF数据绑定

2023-10-07 11:04:58

WPF数据UI

2013-07-05 10:04:47

2010-01-30 11:23:59

2012-07-26 10:29:55

Linux操作系统

2020-08-31 07:30:28

UbuntuRoot密码

2018-12-27 13:35:11

MySQLMySQL 8重置密码

2022-07-21 09:31:58

Actuator密码框架

2022-09-15 00:08:46

密码安全身份验证

2022-05-26 10:30:48

Fedora操作系统root 密码

2011-03-30 09:13:13

静态类Windows Pho

2021-01-03 09:33:48

密码数字身份加密解密

2012-11-30 13:43:30

2022-06-06 06:10:00

密码验证安全

2019-09-09 10:25:54

MySQLMariaDB roo密码

2013-07-17 17:15:57

2009-02-18 20:31:51

2020-04-14 11:48:59

密码网络攻击网络安全

2013-10-08 10:07:58

2010-03-01 17:52:03

WCF选择绑定
点赞
收藏

51CTO技术栈公众号