Created
December 20, 2016 10:59
-
-
Save lindexi/e4809b4b54a36db6aa166524c89fcebb to your computer and use it in GitHub Desktop.
Binding PasswordBox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class PasswordBoxHelper | |
{ | |
public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached( | |
"Password", typeof(string), typeof(PasswordBoxHelper), new PropertyMetadata(default(string),OnPasswordPropertyChanged)); | |
public static void SetPassword(DependencyObject element, string value) | |
{ | |
element.SetValue(PasswordProperty, value); | |
} | |
public static string GetPassword(DependencyObject element) | |
{ | |
return (string) element.GetValue(PasswordProperty); | |
} | |
public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached( | |
"Attach", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(default(bool),Attach)); | |
public static void SetAttach(DependencyObject element, bool value) | |
{ | |
element.SetValue(AttachProperty, value); | |
} | |
public static bool GetAttach(DependencyObject element) | |
{ | |
return (bool) element.GetValue(AttachProperty); | |
} | |
public static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached( | |
"IsUpdating", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(default(bool))); | |
public static void SetIsUpdating(DependencyObject element, bool value) | |
{ | |
element.SetValue(IsUpdatingProperty, value); | |
} | |
public static bool GetIsUpdating(DependencyObject element) | |
{ | |
return (bool) element.GetValue(IsUpdatingProperty); | |
} | |
private static void OnPasswordPropertyChanged(DependencyObject sender, | |
DependencyPropertyChangedEventArgs e) | |
{ | |
PasswordBox passwordBox = sender as PasswordBox; | |
if (passwordBox != null) | |
{ | |
passwordBox.PasswordChanged -= PasswordChanged; | |
if (!(bool)GetIsUpdating(passwordBox)) | |
{ | |
passwordBox.Password = (string)e.NewValue; | |
} | |
passwordBox.PasswordChanged += PasswordChanged; | |
} | |
} | |
private static void Attach(DependencyObject sender, | |
DependencyPropertyChangedEventArgs e) | |
{ | |
PasswordBox 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 PasswordChanged(object sender, RoutedEventArgs e) | |
{ | |
PasswordBox passwordBox = sender as PasswordBox; | |
if (passwordBox != null) | |
{ | |
SetIsUpdating(passwordBox, true); | |
SetPassword(passwordBox, passwordBox.Password); | |
SetIsUpdating(passwordBox, false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment