/// <summary> /// Validates the current changes in the TextBox. Does nothing is there are no changes. /// </summary> public void Validate() { if (IsReadOnly || !HasChangesToValidate || validating) { return; } var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); OnValidating(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } if (!IsTextCompatibleWithValueBinding(Text)) { var textBindingFailedArgs = new RoutedEventArgs(TextToSourceValueConversionFailedEvent); RaiseEvent(textBindingFailedArgs); // We allow this to continue through since it'll revert itself through later code. } validating = true; var coercedText = CoerceTextForValidation(Text); SetCurrentValue(TextProperty, coercedText); BindingExpression expression = GetBindingExpression(TextProperty); try { expression?.UpdateSource(); } catch (TargetInvocationException ex) when(ex.InnerException is InvalidCastException) { var textBindingFailedArgs = new RoutedEventArgs(TextToSourceValueConversionFailedEvent); RaiseEvent(textBindingFailedArgs); } ClearUndoStack(); var validatedArgs = new ValidationRoutedEventArgs <string>(ValidatedEvent, coercedText); OnValidated(); RaiseEvent(validatedArgs); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) { ValidateCommand.Execute(ValidateCommandParameter); } validating = false; HasChangesToValidate = false; }
/// <summary> /// Validates the current changes in the TextBox. /// </summary> public void Validate() { var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); OnValidating(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) return; RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) return; validating = true; var coercedText = CoerceTextForValidation(Text); SetCurrentValue(TextProperty, coercedText); BindingExpression expression = GetBindingExpression(TextProperty); if (expression != null) expression.UpdateSource(); ClearUndoStack(); var validatedArgs = new ValidationRoutedEventArgs<string>(ValidatedEvent, coercedText); OnValidated(); RaiseEvent(validatedArgs); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) ValidateCommand.Execute(ValidateCommandParameter); validating = false; }
private void EditableTextBoxValidated(object sender, ValidationRoutedEventArgs <string> e) { if (!IsDropDownOpen && !clearing && IsKeyboardFocusWithin) { // Setting IsDropDownOpen to true will select all the text. We don't want this behavior, so let's save and restore the caret index. var index = editableTextBox.CaretIndex; IsDropDownOpen = true; editableTextBox.CaretIndex = index; } }
private void EditableTextBoxValidated(object sender, ValidationRoutedEventArgs<string> e) { // This may happens somehow when the template is refreshed. if (!ReferenceEquals(sender, editableTextBox)) return; var validatedArgs = new RoutedEventArgs(ValidatedEvent); RaiseEvent(validatedArgs); if (ClearTextAfterValidation) { clearing = true; editableTextBox.Text = string.Empty; clearing = false; } }
/// <summary> /// Validates the current changes in the TextBox. Does nothing is there are no changes. /// </summary> public void Validate() { if (IsReadOnly || !HasChangesToValidate) { return; } var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); OnValidating(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } validating = true; var coercedText = CoerceTextForValidation(Text); SetCurrentValue(TextProperty, coercedText); BindingExpression expression = GetBindingExpression(TextProperty); expression?.UpdateSource(); ClearUndoStack(); var validatedArgs = new ValidationRoutedEventArgs <string>(ValidatedEvent, coercedText); OnValidated(); RaiseEvent(validatedArgs); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) { ValidateCommand.Execute(ValidateCommandParameter); } validating = false; HasChangesToValidate = false; }