PasswordHelper.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. namespace Muchinfo.WPF.Controls.Password
  4. {
  5. public static class PasswordHelper
  6. {
  7. #region Fields
  8. public static readonly DependencyProperty AttachProperty =
  9. DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));
  10. public static readonly DependencyProperty IsUpdatingProperty =
  11. DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), typeof(PasswordHelper));
  12. public static readonly DependencyProperty PasswordProperty =
  13. DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper), new FrameworkPropertyMetadata(string.Empty,
  14. OnPasswordPropertyChanged));
  15. #endregion Fields
  16. #region Methods
  17. #region Public Static Methods
  18. public static bool GetAttach(UIElement element)
  19. {
  20. return (bool)element.GetValue(AttachProperty);
  21. }
  22. public static bool GetIsUpdating(UIElement element)
  23. {
  24. return (bool)element.GetValue(IsUpdatingProperty);
  25. }
  26. public static string GetPassword(UIElement element)
  27. {
  28. return (string)element.GetValue(PasswordProperty);
  29. }
  30. public static void SetAttach(UIElement element, bool value)
  31. {
  32. element.SetValue(AttachProperty, value);
  33. }
  34. public static void SetIsUpdating(UIElement element, bool value)
  35. {
  36. element.SetValue(IsUpdatingProperty, value);
  37. }
  38. public static void SetPassword(UIElement element, string value)
  39. {
  40. element.SetValue(PasswordProperty, value);
  41. }
  42. #endregion Public Static Methods
  43. #region Private Static Methods
  44. private static void Attach(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  45. {
  46. var passwordBox = sender as PasswordBox;
  47. if (passwordBox == null) return;
  48. if ((bool)e.OldValue)
  49. {
  50. passwordBox.PasswordChanged -= PasswordChanged;
  51. }
  52. if ((bool)e.NewValue)
  53. {
  54. passwordBox.PasswordChanged += PasswordChanged;
  55. }
  56. }
  57. private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  58. {
  59. var passwordBox = d as PasswordBox;
  60. if (passwordBox == null) return;
  61. passwordBox.PasswordChanged -= PasswordChanged;
  62. if (!(bool)GetIsUpdating(passwordBox))
  63. {
  64. passwordBox.Password = (string)e.NewValue;
  65. }
  66. passwordBox.PasswordChanged += PasswordChanged;
  67. }
  68. private static void PasswordChanged(object sender, RoutedEventArgs e)
  69. {
  70. var passwordBox = sender as PasswordBox;
  71. if (passwordBox == null) return;
  72. SetIsUpdating(passwordBox, true);
  73. SetPassword(passwordBox, passwordBox.Password);
  74. SetIsUpdating(passwordBox, false);
  75. }
  76. #endregion Private Static Methods
  77. #endregion Methods
  78. }
  79. }