Guest User

Untitled

a guest
Oct 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. <PasswordBox x:Name="TboxPassword" Grid.Row="1" Grid.Column="0"
  2. controls:TextboxHelper.Watermark="Password ..."
  3. controls:TextboxHelper.ClearTextButton="True"
  4. Margin="10, 10, 0, 0">
  5. <i:Interaction.Behaviors>
  6. <misc:PasswordBoxBehavior Password="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>
  7. </i:Interaction.Behaviors>
  8. </PasswordBox>
  9.  
  10. public class PasswordBoxBehavior : Behavior<PasswordBox>
  11. {
  12. #region Fields
  13.  
  14. private readonly object _tryToExecuteActionSyncObject = new object();
  15. private bool _isUpdating;
  16.  
  17. #endregion
  18.  
  19. #region Properties
  20.  
  21. public string Password
  22. {
  23. get { return (string)GetValue(PasswordProperty); }
  24. set { SetValue(PasswordProperty, value); }
  25. }
  26.  
  27. public static readonly DependencyProperty PasswordProperty =
  28. DependencyProperty.Register("Password", typeof(string), typeof(PasswordBoxBehavior),
  29. new PropertyMetadata(string.Empty, OnPasswordPropertyChanged));
  30.  
  31. #endregion
  32.  
  33. #region Methods
  34.  
  35. protected override void OnAttached()
  36. {
  37. base.OnAttached();
  38.  
  39. AssociatedObject.PasswordChanged += OnAssociatedObjectPasswordChanged;
  40. }
  41.  
  42. protected override void OnDetaching()
  43. {
  44. base.OnDetaching();
  45.  
  46. AssociatedObject.PasswordChanged -= OnAssociatedObjectPasswordChanged;
  47. }
  48.  
  49. private void OnAssociatedObjectPasswordChanged(object sender, RoutedEventArgs e)
  50. {
  51. TryToExecuteAction(() => Password = AssociatedObject == null
  52. ? string.Empty
  53. : AssociatedObject.Password);
  54. }
  55.  
  56. private static void OnPasswordPropertyChanged
  57. (DependencyObject sender, DependencyPropertyChangedEventArgs e)
  58. {
  59. PasswordBoxBehavior passwordBoxBehavior;
  60. if (sender == null
  61. || (passwordBoxBehavior = sender as PasswordBoxBehavior) == null
  62. || passwordBoxBehavior.AssociatedObject == null)
  63. {
  64. return;
  65. }
  66.  
  67. passwordBoxBehavior.TryToExecuteAction
  68. (() => passwordBoxBehavior.AssociatedObject.Password =
  69. (e.NewValue == null
  70. ? string.Empty
  71. : (string) e.NewValue));
  72. }
  73.  
  74. private void TryToExecuteAction(Action actionToExecute)
  75. {
  76. bool continueExecution;
  77. lock (_tryToExecuteActionSyncObject)
  78. {
  79. continueExecution = _isUpdating == false;
  80. _isUpdating = true;
  81. }
  82.  
  83. if (continueExecution == false)
  84. {
  85. return;
  86. }
  87.  
  88. try
  89. {
  90. if (actionToExecute != null)
  91. {
  92. actionToExecute();
  93. }
  94. }
  95. finally
  96. {
  97. lock (_tryToExecuteActionSyncObject)
  98. {
  99. _isUpdating = false;
  100. }
  101. }
  102. }
  103.  
  104. #endregion
  105. }
  106.  
  107. {"Cannot add instance of type 'PasswordBoxBehavior' to a collection of type 'BehaviorCollection'. Only items of type 'T' are allowed."}
Add Comment
Please, Sign In to add comment