public CellValidationError( CellValidationRule ruleInError, Cell cellInError, object errorContent, Exception exception ) { m_ruleInError = ruleInError; m_cellInError = cellInError; m_errorContent = errorContent; m_exception = exception; }
private ValidationResult ValidateCellRules( out Exception validationException, out CellValidationRule ruleInError ) { ValidationResult result = ValidationResult.ValidResult; ruleInError = null; validationException = null; CellValidationContext cellValidationContext = new CellValidationContext( this.GetRealDataContext(), this ); CultureInfo culture = this.Language.GetSpecificCulture(); foreach( CellValidationRule cellValidationRule in this.CellValidationRules ) { try { result = cellValidationRule.Validate( this.Content, culture, cellValidationContext ); } catch( Exception exception ) { validationException = exception; result = new ValidationResult( false, exception.Message ); } if( !result.IsValid ) { ruleInError = cellValidationRule; break; } } ColumnBase parentColumn = this.ParentColumn; if( ( parentColumn != null ) && ( result.IsValid ) ) { foreach( CellValidationRule cellValidationRule in parentColumn.CellValidationRules ) { try { result = cellValidationRule.Validate( this.Content, culture, cellValidationContext ); } catch( Exception exception ) { validationException = exception; result = new ValidationResult( false, exception.Message ); } if( !result.IsValid ) { ruleInError = cellValidationRule; break; } } } return result; }
private ValidationResult ValidateContentBindingRules( out Exception validationException, out CellValidationRule ruleInErrorWrapper ) { validationException = null; ruleInErrorWrapper = null; ValidationError cellContentBindingValidationError = this.GetContentBindingValidationError(); if( cellContentBindingValidationError == null ) return ValidationResult.ValidResult; ruleInErrorWrapper = new CellContentBindingValidationRule( cellContentBindingValidationError.RuleInError ); validationException = cellContentBindingValidationError.Exception; return new ValidationResult( false, cellContentBindingValidationError.ErrorContent ); }
private void SetAllError( ValidationResult result, Exception validationException, CellValidationRule ruleInError ) { bool invalid = ( !result.IsValid ); if( invalid ) { CellValidationError validationError = new CellValidationError( ruleInError, this, result.ErrorContent, validationException ); this.SetValidationError( validationError ); } else { this.SetValidationError( null ); } }
internal ValidationResult ValidateCellEditorRules( out Exception validationException, out CellValidationRule ruleInError ) { validationException = null; ruleInError = null; FrameworkElement cellEditorBoundControl = this.CellEditorBoundControl; ValidationResult validationResult = Cell.CellEditorErrorValidationRule.Validate( this.Content, CultureInfo.CurrentCulture, new CellValidationContext( this.GetRealDataContext(), this ), cellEditorBoundControl ); if( !validationResult.IsValid ) { ruleInError = Cell.CellEditorErrorValidationRule; validationException = new DataGridException( "An invalid or incomplete value was provided." ); } return validationResult; }
internal ValidationResult ValidateAndSetAllErrors( bool validateCellEditorRules, bool validateUIRules, bool updateContentBindingSource, bool cascadeValidate, out Exception exception, out CellValidationRule ruleInError ) { System.Diagnostics.Debug.Assert( !this.PreventValidateAndSetAllErrors, "We should not have called this method while in this state." ); System.Diagnostics.Debug.Assert( !( cascadeValidate && this.IsInCascadingValidation ), "We should never be calling ValidateAndSetAllErrors with a request to start cascade validation if we are currently cascading validation." ); if( this.IsInCascadingValidation && cascadeValidate ) throw new DataGridInternalException(); ValidationResult result = ValidationResult.ValidResult; exception = null; ruleInError = null; // Validate that the CellEditor isn't in error. if( validateCellEditorRules ) { result = this.ValidateCellEditorRules( out exception, out ruleInError ); // If the CellEditor is in error, it must be shown, no matter what. if( !result.IsValid ) { this.SetAllError( result, exception, ruleInError ); return result; } } // Validate CellValidationRules against the cell's Content property. if( validateUIRules ) result = this.ValidateCellRules( out exception, out ruleInError ); if( result.IsValid ) { // Only need to update the Content binding source if the cell is dirty or if we are cascading the validation. if( ( updateContentBindingSource ) && ( ( this.IsDirty ) || ( this.IsDirtyFromInitializingInsertionRow ) || ( this.IsInCascadingValidation ) ) ) { // Update the Content binding's source and check for errors. result = this.UpdateContentBindingSource( out exception, out ruleInError ); } else { // Just check for errors. // We must validate even if the cell isn't dirty since its value might have been in error even before // entering edit on the row. // ie: DataErrorInfo or any other non-restrictive validation error. result = this.ValidateContentBindingRules( out exception, out ruleInError ); } } // Only refresh ValidationError property, HasValidationError property, and the cell's style // if if we are in a validating the UI Rules or if there wasn't any UI error before this validation pass. if( ( validateUIRules ) || ( !this.HasUIValidationError ) ) { this.SetAllError( result, exception, ruleInError ); if( cascadeValidate ) this.CascadeValidation(); } return result; }
internal ValidationResult UpdateContentBindingSource( out Exception exception, out CellValidationRule ruleInErrorWrapper ) { Debug.Assert( ( ( this.IsDirty ) || ( this.IsDirtyFromInitializingInsertionRow ) || ( this.IsInCascadingValidation ) ), "UpdateContentBindingSource should not be called when the cell isn't dirty beside when cascading validation. Call ValidateContentBindingRules instead." ); exception = null; ruleInErrorWrapper = null; ValidationResult validationResult = ValidationResult.ValidResult; BindingExpressionBase contentBindingExpression = this.GetContentBindingExpression(); if( contentBindingExpression != null ) { // The caller of UpdateContentBindingSource will take care of setting the errors. this.PreventValidateAndSetAllErrors = true; this.IsUpdatingContentBindingSource = true; try { contentBindingExpression.UpdateSource(); } finally { this.PreventValidateAndSetAllErrors = false; this.IsUpdatingContentBindingSource = false; } validationResult = this.ValidateContentBindingRules( out exception, out ruleInErrorWrapper ); } // Only consider the content committed if the source was correctly updated. if( ( validationResult.IsValid ) || ( !( Cell.GetIsRuleInErrorRestrictive( ruleInErrorWrapper ) ) ) ) { this.ContentCommitted(); //Update StatsCells right away. this.InvalidateStatsFunctions(); } // The dirty flag will only be lowered when the row ends or cancels edition, or if the cell cancels edition and it wasn't dirty // when begining edition. return validationResult; }
internal static bool GetIsRuleInErrorRestrictive( CellValidationRule ruleInError ) { if( ruleInError == null ) return false; CellContentBindingValidationRule cellContentBindingValidationRule = ruleInError as CellContentBindingValidationRule; if( cellContentBindingValidationRule != null ) return Cell.GetIsRuleInErrorRestrictive( cellContentBindingValidationRule.ValidationRule ); return true; }