/// <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; }
private void Validate() { var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); AssociatedObject.RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } BindingExpression expression = AssociatedObject.GetBindingExpression(TextBox.TextProperty); if (expression != null) { expression.UpdateSource(); } ClearUndoStack(); AssociatedObject.RaiseEvent(new RoutedEventArgs(ValidatedEvent)); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) { ValidateCommand.Execute(ValidateCommandParameter); } }
/// <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; }
/// <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; }