Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. namespace PlumCONF2.Utils.Behaviours
  2. {
  3. public class PasswordBoxBehavior : Behavior<PasswordBox>
  4. {
  5. public bool ResetPassword
  6. {
  7. get { return (bool)GetValue(ResetPasswordProperty); }
  8. set { SetValue(ResetPasswordProperty, value); }
  9. }
  10.  
  11. // Using a DependencyProperty as the backing store for ResetPassword. This enables animation, styling, binding, etc...
  12. public static readonly DependencyProperty ResetPasswordProperty =
  13. DependencyProperty.Register("ResetPassword", typeof(bool), typeof(PasswordBoxBehavior), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnResetPasswordChanged));
  14.  
  15. static void OnResetPasswordChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
  16. {
  17. PasswordBoxBehavior behavior = depObj as PasswordBoxBehavior;
  18. PasswordBox item = behavior.AssociatedObject as PasswordBox;
  19. if (item == null)
  20. return;
  21.  
  22. if ((bool)e.NewValue)
  23. item.Password = string.Empty;
  24.  
  25. behavior.ResetPassword = false;
  26. }
  27.  
  28. private bool isRoutedEventHandlerAssign;
  29. public string Text
  30. {
  31. get { return (string)GetValue(TextProperty); }
  32. set { SetValue(TextProperty, value); }
  33. }
  34.  
  35. // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
  36. public static readonly DependencyProperty TextProperty =
  37. DependencyProperty.Register("Text", typeof(string), typeof(PasswordBoxBehavior), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged));
  38.  
  39. static void OnTextChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
  40. {
  41. PasswordBoxBehavior behavior = depObj as PasswordBoxBehavior;
  42. PasswordBox item = behavior.AssociatedObject as PasswordBox;
  43. if (item == null)
  44. return;
  45.  
  46. if (item.Password != e.NewValue as string)
  47. {
  48. item.Password = e.NewValue as string;
  49. }
  50.  
  51. if (!behavior.isRoutedEventHandlerAssign)
  52. {
  53. item.PasswordChanged += (sender, eArg) =>
  54. {
  55. behavior.Text = item.Password;
  56. };
  57. behavior.isRoutedEventHandlerAssign = true;
  58. }
  59. }
  60.  
  61. public PasswordBoxBehavior()
  62. {
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement