private static void HandlePasswordChanged(object sender, RoutedEventArgs e) { var box = sender as PasswordBox; // set a flag to indicate that we're updating the password UpdatingPassword.SetValue(box, true); // push the new password into the BoundPassword property SetValue(box, box.Password); UpdatingPassword.SetValue(box, false); }
public override void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var box = sender as PasswordBox; // only handle this event when the property is attached to a PasswordBox // and when the BindPassword attached property has been set to true if (sender == null || !BindPassword.GetValue(sender)) { return; } // avoid recursive updating by ignoring the box's changed event box.PasswordChanged -= HandlePasswordChanged; var newPassword = (string)e.NewValue; if (!UpdatingPassword.GetValue(box)) { box.Password = newPassword; } box.PasswordChanged += HandlePasswordChanged; }