Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #region
  2.  
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6.  
  7. #endregion
  8.  
  9. namespace Cmc.Installer.Core.Controls
  10. {
  11. public sealed class BindablePasswordBox : Decorator
  12. {
  13. /// <summary>
  14. /// The password dependency property.
  15. /// </summary>
  16. public static readonly DependencyProperty PasswordProperty;
  17. public static readonly DependencyProperty BorderBrushProperty;
  18.  
  19. private readonly RoutedEventHandler _savedCallback;
  20. private bool _isPreventCallback;
  21.  
  22. /// <summary>
  23. /// Static constructor to initialize the dependency properties.
  24. /// </summary>
  25. static BindablePasswordBox()
  26. {
  27. PasswordProperty = DependencyProperty.Register(
  28. "Password",
  29. typeof (string),
  30. typeof (BindablePasswordBox),
  31. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
  32. OnPasswordPropertyChanged)
  33. );
  34.  
  35. BorderBrushProperty = DependencyProperty.Register(
  36. "BorderBrush",
  37. typeof(Brush),
  38. typeof(BindablePasswordBox),
  39. new PropertyMetadata(default(Brush))
  40. );
  41. }
  42.  
  43. /// <summary>
  44. /// Saves the password changed callback and sets the child element to the password box.
  45. /// </summary>
  46. public BindablePasswordBox()
  47. {
  48. _savedCallback = HandlePasswordChanged;
  49.  
  50. var passwordBox = new PasswordBox();
  51. passwordBox.PasswordChanged += _savedCallback;
  52. Child = passwordBox;
  53. }
  54.  
  55. /// <summary>
  56. /// The password dependency property.
  57. /// </summary>
  58. public string Password
  59. {
  60. get { return GetValue(PasswordProperty) as string; }
  61. set { SetValue(PasswordProperty, value); }
  62. }
  63.  
  64. public Brush BorderBrush
  65. {
  66. get { return (Brush)GetValue(BorderBrushProperty); }
  67. set { SetValue(BorderBrushProperty, value); }
  68. }
  69.  
  70. /// <summary>
  71. /// Handles changes to the password dependency property.
  72. /// </summary>
  73. /// <param name="d">the dependency object</param>
  74. /// <param name="eventArgs">the event args</param>
  75. private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs eventArgs)
  76. {
  77. var bindablePasswordBox = (BindablePasswordBox) d;
  78. var passwordBox = (PasswordBox) bindablePasswordBox.Child;
  79.  
  80. if (bindablePasswordBox._isPreventCallback)
  81. {
  82. return;
  83. }
  84.  
  85. passwordBox.PasswordChanged -= bindablePasswordBox._savedCallback;
  86. passwordBox.Password = (eventArgs.NewValue != null) ? eventArgs.NewValue.ToString() : "";
  87. passwordBox.PasswordChanged += bindablePasswordBox._savedCallback;
  88. }
  89.  
  90. /// <summary>
  91. /// Handles the password changed event.
  92. /// </summary>
  93. /// <param name="sender">the sender</param>
  94. /// <param name="eventArgs">the event args</param>
  95. private void HandlePasswordChanged(object sender, RoutedEventArgs eventArgs)
  96. {
  97. var passwordBox = (PasswordBox) sender;
  98.  
  99. _isPreventCallback = true;
  100. Password = passwordBox.Password;
  101. _isPreventCallback = false;
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement