private static void SetPasswordBoxSelection(PasswordBox passwordBox, int start, int length) { var select = passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic); select.Invoke(passwordBox, new object[] { start, length }); }
private static void SetPasswordBoxSelection(System.Windows.Controls.PasswordBox passwordBox, int start, int length) { var select = passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic); select.Invoke(passwordBox, new object[] { start, length }); }
private void SetSelection(PasswordBox passwordBox, int start, int length) { try { if (start < 0x0) { start = passwordBox.Password.Length; } passwordBox.GetType().GetMethod("Select", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(passwordBox, new object[] { start, length }); } catch (Exception exception) { DebugLog.Assert(false, exception.Message); passwordBox.SelectAll(); } }
public PasswordBox() { passwordBox = new System.Windows.Controls.PasswordBox(); passwordBox.Padding = new System.Windows.Thickness(5); passwordBox.VerticalContentAlignment = System.Windows.VerticalAlignment.Center; passwordBox.Tag = this; passwordBox.PasswordChanged += (o, e) => { if (passwordBox.Password == currentValue) { // password boxes have a peculiar behavior where the cursor is pushed to the beginning // when used in a two-way binding. to compensate, we need to force the cursor to the end. var select = passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic); if (select != null) { select.Invoke(passwordBox, new object[] { currentValue.Length, 0 }); } return; } if (!(expression == null || expression.IsMatch(passwordBox.Password))) { passwordBox.Password = currentValue; var select = passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic); if (select != null) { select.Invoke(passwordBox, new object[] { currentValue.Length, 0 }); } return; } string oldValue = currentValue; currentValue = passwordBox.Password; var phandler = PropertyChanged; if (phandler != null) { phandler(this, new PropertyChangedEventArgs("Password")); phandler(this, new PropertyChangedEventArgs("StringValue")); } var handler = PasswordChanged; if (handler != null) { handler(Pair ?? this, new ValueChangedEventArgs <string>(oldValue, currentValue)); } }; passwordBox.PreviewKeyDown += (o, e) => { if (e.Key == System.Windows.Input.Key.Return) { var handler = ReturnKeyPressed; if (handler != null) { var args = new EventHandledEventArgs(); handler(pair ?? this, args); e.Handled = args.IsHandled; } } }; passwordBox.Loaded += (o, e) => { if (setFocusOnLoad) { passwordBox.Focus(); } }; passwordBox.GotFocus += (o, e) => { var phandler = PropertyChanged; if (phandler != null) { phandler(this, new PropertyChangedEventArgs("IsFocused")); } var handler = GotFocus; if (handler != null) { handler(pair ?? this, EventArgs.Empty); } }; passwordBox.LostFocus += (o, e) => { var phandler = PropertyChanged; if (phandler != null) { phandler(this, new PropertyChangedEventArgs("IsFocused")); } var handler = LostFocus; if (handler != null) { handler(pair ?? this, EventArgs.Empty); } }; }