public PasswordBoxBehavior() { this.onPasswordChanged = (s, e) => { this.isPushing = true; this.Text = this.AssociatedObject.Password; var text = BindingOperations.GetBindingExpression(this, PasswordBoxBehavior.TextProperty); var tag = BindingOperations.GetBindingExpression(this.AssociatedObject, PasswordBox.TagProperty); if (text.HasError) { System.Windows.Controls.Validation.MarkInvalid(tag, text.ValidationError); } else { System.Windows.Controls.Validation.ClearInvalid(tag); } this.isPushing = false; }; onPreviewKeyDown = (s, e) => { var d = ( DependencyObject )s; if (this.Command != null) { var cmd = this.Command; var prm = this.CommandParameter; var gestures = cmd.GetGestures(); var senderGestures = gestures.Where(gesture => gesture.Matches(d, e)); if (((gestures.None() && e.Key == System.Windows.Input.Key.Enter) || senderGestures.Any()) && cmd.CanExecute(prm)) { var k = e.Key; var m = ModifierKeys.None; if (senderGestures.Any()) { var gesture = senderGestures.First(); var keygesture = gesture as KeyGesture; if (keygesture != null) { k = keygesture.Key; m = keygesture.Modifiers; } } var args = new PasswordBoxCommandArgs(k, m, prm); cmd.Execute(args); e.Handled = true; } } }; }
static Password() { onLoaded = (s, e) => { var box = ( PasswordBox )s; box.PasswordChanged += onPasswordChanged; box.PreviewKeyDown += onPreviewKeyDown; var newPassword = GetText(box); SetPasswordOnPasswordBox(box, newPassword); }; onUnloaded = (s, e) => { var box = ( PasswordBox )s; box.PasswordChanged -= onPasswordChanged; box.PreviewKeyDown -= onPreviewKeyDown; }; onPasswordChanged = (s, e) => { PasswordBox box = ( PasswordBox )s; Password.SetIsUpdating(box, true); Password.SetText(box, box.Password); Password.SetIsUpdating(box, false); }; onPreviewKeyDown = (s, e) => { var d = ( PasswordBox )s; var cmd = GetCommand(d); if (cmd != null) { var prm = GetCommandParameter(d); var gestures = cmd.GetGestures(); var senderGestures = gestures.Where(gesture => gesture.Matches(d, e)); if (((gestures.None() && e.Key == System.Windows.Input.Key.Enter) || senderGestures.Any()) && cmd.CanExecute(prm)) { var k = e.Key; var m = ModifierKeys.None; if (senderGestures.Any()) { var gesture = senderGestures.First(); var keygesture = gesture as KeyGesture; if (keygesture != null) { k = keygesture.Key; m = keygesture.Modifiers; } } var args = new PasswordBoxCommandArgs(k, m, prm); cmd.Execute(args); e.Handled = true; } } }; }