终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

74 lines
2.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. namespace BPASmart.UserManagement.Helper
  9. {
  10. /// <summary>
  11. /// 给PasswordBox添加一个可以绑定的附加类
  12. /// </summary>
  13. public class PasswordHelper
  14. {
  15. static bool IsUpdete = false;
  16. #region PasswordProperty
  17. public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string),
  18. typeof(PasswordHelper), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnPropertyChanged)));
  19. public static string GetPassword(DependencyObject d)
  20. {
  21. return d.GetValue(PasswordProperty).ToString();
  22. }
  23. public static void SetPassword(DependencyObject d, string value)
  24. {
  25. d.SetValue(PasswordProperty, value);
  26. }
  27. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  28. {
  29. PasswordBox password = d as PasswordBox;
  30. password.PasswordChanged -= password_PasswordChanged;
  31. if (!IsUpdete) password.Password = e.NewValue?.ToString();
  32. password.PasswordChanged += password_PasswordChanged;
  33. }
  34. #endregion
  35. #region AttachProperty
  36. public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(bool),
  37. typeof(PasswordHelper), new FrameworkPropertyMetadata(default(bool), new PropertyChangedCallback(OnAttachChanged)));
  38. public static bool GetAttach(DependencyObject d)
  39. {
  40. return (bool)d.GetValue(AttachProperty);
  41. }
  42. public static void SetAttach(DependencyObject d, bool value)
  43. {
  44. d.SetValue(AttachProperty, value);
  45. }
  46. private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  47. {
  48. PasswordBox password = d as PasswordBox;
  49. password.PasswordChanged += password_PasswordChanged;
  50. }
  51. private static void password_PasswordChanged(object sender, RoutedEventArgs e)
  52. {
  53. PasswordBox passwordBox = sender as PasswordBox;
  54. IsUpdete = true;
  55. SetPassword(passwordBox, passwordBox.Password);
  56. IsUpdete = false;
  57. }
  58. #endregion
  59. }
  60. }