| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System.Windows;
- using System.Windows.Controls;
- namespace Muchinfo.WPF.Controls.Password
- {
- public static class PasswordHelper
- {
- #region Fields
- public static readonly DependencyProperty AttachProperty =
- DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach));
- public static readonly DependencyProperty IsUpdatingProperty =
- DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), typeof(PasswordHelper));
- public static readonly DependencyProperty PasswordProperty =
- DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper), new FrameworkPropertyMetadata(string.Empty,
- OnPasswordPropertyChanged));
- #endregion Fields
- #region Methods
- #region Public Static Methods
- public static bool GetAttach(UIElement element)
- {
- return (bool)element.GetValue(AttachProperty);
- }
- public static bool GetIsUpdating(UIElement element)
- {
- return (bool)element.GetValue(IsUpdatingProperty);
- }
- public static string GetPassword(UIElement element)
- {
- return (string)element.GetValue(PasswordProperty);
- }
- public static void SetAttach(UIElement element, bool value)
- {
- element.SetValue(AttachProperty, value);
- }
- public static void SetIsUpdating(UIElement element, bool value)
- {
- element.SetValue(IsUpdatingProperty, value);
- }
- public static void SetPassword(UIElement element, string value)
- {
- element.SetValue(PasswordProperty, value);
- }
- #endregion Public Static Methods
- #region Private Static Methods
- private static void Attach(DependencyObject sender, DependencyPropertyChangedEventArgs e)
- {
- var passwordBox = sender as PasswordBox;
- if (passwordBox == null) return;
- if ((bool)e.OldValue)
- {
- passwordBox.PasswordChanged -= PasswordChanged;
- }
- if ((bool)e.NewValue)
- {
- passwordBox.PasswordChanged += PasswordChanged;
- }
- }
- private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var passwordBox = d as PasswordBox;
- if (passwordBox == null) return;
- passwordBox.PasswordChanged -= PasswordChanged;
- if (!(bool)GetIsUpdating(passwordBox))
- {
- passwordBox.Password = (string)e.NewValue;
- }
- passwordBox.PasswordChanged += PasswordChanged;
- }
- private static void PasswordChanged(object sender, RoutedEventArgs e)
- {
- var passwordBox = sender as PasswordBox;
- if (passwordBox == null) return;
- SetIsUpdating(passwordBox, true);
- SetPassword(passwordBox, passwordBox.Password);
- SetIsUpdating(passwordBox, false);
- }
- #endregion Private Static Methods
- #endregion Methods
- }
- }
|