PasswordBoxHelper.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. namespace PDF_Master.Helper
  4. {
  5. public class PasswordBoxHelper
  6. {
  7. // 依赖附加属性 Password Attach DisplayText IsWrongValue
  8. public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordBoxHelper),
  9. new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
  10. public static string GetPassword(DependencyObject d)
  11. {
  12. return (string)d.GetValue(PasswordProperty);
  13. }
  14. public static void SetPassword(DependencyObject d, string value)
  15. {
  16. d.SetValue(PasswordProperty, value);
  17. }
  18. public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(string), typeof(PasswordBoxHelper),
  19. new PropertyMetadata(new PropertyChangedCallback(OnAttachChanged)));
  20. public static string GetAttach(DependencyObject d)
  21. {
  22. return (string)d.GetValue(PasswordProperty);
  23. }
  24. public static void SetAttach(DependencyObject d, string value)
  25. {
  26. d.SetValue(PasswordProperty, value);
  27. }
  28. public static readonly DependencyProperty DisplayTextProperty = DependencyProperty.RegisterAttached("DisplayText", typeof(string), typeof(PasswordBoxHelper));
  29. public static string GetDisplayText(DependencyObject d)
  30. {
  31. return (string)d.GetValue(PasswordProperty);
  32. }
  33. public static void SetDisplayText(DependencyObject d, string value)
  34. {
  35. d.SetValue(PasswordProperty, value);
  36. }
  37. public static readonly DependencyProperty IsRightValueProperty = DependencyProperty.RegisterAttached("IsRightValue", typeof(string), typeof(PasswordBoxHelper));
  38. public static string GetIsRightValue(DependencyObject d)
  39. {
  40. return (string)d.GetValue(PasswordProperty);
  41. }
  42. public static void SetIsRightValue(DependencyObject d, string value)
  43. {
  44. d.SetValue(PasswordProperty, value);
  45. }
  46. static bool _isUpdating = false;
  47. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  48. {
  49. PasswordBox pb = (d as PasswordBox);
  50. pb.PasswordChanged -= Pb_PasswordChanged;
  51. if (!_isUpdating)
  52. (d as PasswordBox).Password = e.NewValue.ToString();
  53. pb.PasswordChanged += Pb_PasswordChanged;
  54. }
  55. private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  56. {
  57. PasswordBox pb = (d as PasswordBox);
  58. pb.PasswordChanged += Pb_PasswordChanged;
  59. }
  60. private static void Pb_PasswordChanged(object sender, RoutedEventArgs e)
  61. {
  62. PasswordBox pb = (sender as PasswordBox);
  63. _isUpdating = true;
  64. SetPassword(pb, pb.Password);
  65. _isUpdating = false;
  66. }
  67. }
  68. }