Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Controls / DataErrorValidationRule.cs / 1305600 / DataErrorValidationRule.cs
//---------------------------------------------------------------------------- // //// Copyright (C) by Microsoft Corporation. All rights reserved. // // // // Description: // DataErrorValidationRule is used when a ValidationError is the result of // a data error in the source item itself (e.g. as exposed by IDataErrorInfo). // //--------------------------------------------------------------------------- using System; using System.Collections; using System.ComponentModel; using System.Diagnostics; using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using MS.Internal; namespace System.Windows.Controls { ////// DataErrorValidationRule can be added to the ValidationRulesCollection of a Binding /// or MultiBinding to indicate that data errors in the source object should /// be considered ValidationErrors /// public sealed class DataErrorValidationRule : ValidationRule { ////// DataErrorValidationRule ctor. /// public DataErrorValidationRule() : base(ValidationStep.UpdatedValue, true) { } ////// Validate is called when Data binding is updating /// public override ValidationResult Validate(object value, CultureInfo cultureInfo) { // This rule is called during the CommittedValue step, so the value is the // owner of the rule collection - either a BindingGroup or an individual // binding expression. BindingGroup bindingGroup; BindingExpression bindingExpr; if ((bindingGroup = value as BindingGroup) != null) { // in a BindingGroup, check the item-level IDataErrorInfo for each // source item in the group IList items = bindingGroup.Items; for (int i=items.Count-1; i>=0; --i) { IDataErrorInfo idei = items[i] as IDataErrorInfo; if (idei != null) { string error = idei.Error; if (!String.IsNullOrEmpty(error)) { return new ValidationResult(false, error); } } } } else if ((bindingExpr = value as BindingExpression) != null) { // in a binding, check the error info for the binding's source // property IDataErrorInfo idei = bindingExpr.SourceItem as IDataErrorInfo; string name = (idei != null) ? bindingExpr.SourcePropertyName : null; if (!String.IsNullOrEmpty(name)) { // get the data error information, if any, by calling idie[name]. // We do this in a paranoid way, even though indexers with // string-valued arguments are not supposed to throw exceptions. // PreSharp uses message numbers that the C# compiler doesn't know about. // Disable the C# complaints, per the PreSharp documentation. #pragma warning disable 1634, 1691 // PreSharp complains about catching NullReference (and other) exceptions. // It doesn't recognize that IsCritical[Application]Exception() handles these correctly. #pragma warning disable 56500 string error; try { error = idei[name]; } catch (Exception ex) { if (CriticalExceptions.IsCriticalApplicationException(ex)) throw; error = null; if (TraceData.IsEnabled) { TraceData.Trace(TraceEventType.Error, TraceData.DataErrorInfoFailed( name, idei.GetType().FullName, ex.GetType().FullName, ex.Message), bindingExpr); } } #pragma warning restore 56500 #pragma warning restore 1634, 1691 if (!String.IsNullOrEmpty(error)) { return new ValidationResult(false, error); } } } else throw new InvalidOperationException(SR.Get(SRID.ValidationRule_UnexpectedValue, this, value)); return ValidationResult.ValidResult; } internal static readonly DataErrorValidationRule Instance = new DataErrorValidationRule(); } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Int32Rect.cs
- FontDialog.cs
- _ProxyChain.cs
- BuildResult.cs
- ElementAtQueryOperator.cs
- AsymmetricAlgorithm.cs
- Int32CollectionConverter.cs
- XPathDocumentBuilder.cs
- Nodes.cs
- TypeConverterMarkupExtension.cs
- System.Data.OracleClient_BID.cs
- SqlStream.cs
- MouseEvent.cs
- TrailingSpaceComparer.cs
- ResourceBinder.cs
- Propagator.ExtentPlaceholderCreator.cs
- ParallelTimeline.cs
- BrowsableAttribute.cs
- DataContractSerializerOperationBehavior.cs
- CodeDelegateCreateExpression.cs
- ZipIOExtraFieldPaddingElement.cs
- BindableTemplateBuilder.cs
- StaticExtensionConverter.cs
- FactoryGenerator.cs
- RealizationDrawingContextWalker.cs
- TagMapInfo.cs
- CssTextWriter.cs
- MemberInfoSerializationHolder.cs
- WorkflowRequestContext.cs
- SmiContextFactory.cs
- Label.cs
- ResourceExpressionBuilder.cs
- AmbientEnvironment.cs
- Util.cs
- DesignerDataConnection.cs
- DataSourceExpressionCollection.cs
- DesigntimeLicenseContextSerializer.cs
- NavigationPropertyEmitter.cs
- ExpressionLink.cs
- Rect3DConverter.cs
- ToolStripItemClickedEventArgs.cs
- PackageRelationship.cs
- SizeKeyFrameCollection.cs
- RTLAwareMessageBox.cs
- WindowsStreamSecurityUpgradeProvider.cs
- Point.cs
- CriticalFinalizerObject.cs
- NegationPusher.cs
- ClientBuildManager.cs
- NodeInfo.cs
- NetworkInterface.cs
- Keyboard.cs
- DmlSqlGenerator.cs
- VectorAnimationBase.cs
- ToolboxDataAttribute.cs
- HttpChannelListener.cs
- ExtractorMetadata.cs
- PersonalizationEntry.cs
- CompiledRegexRunner.cs
- MDIWindowDialog.cs
- FontFamilyIdentifier.cs
- DatagridviewDisplayedBandsData.cs
- NullableConverter.cs
- BulletChrome.cs
- XPathPatternParser.cs
- TrustManagerPromptUI.cs
- TableLayoutPanelCellPosition.cs
- OverflowException.cs
- ObjectStateEntryOriginalDbUpdatableDataRecord.cs
- OdbcParameterCollection.cs
- ConnectionPoint.cs
- FrameworkElementFactoryMarkupObject.cs
- TypeConverterHelper.cs
- counter.cs
- _TimerThread.cs
- ControlCollection.cs
- SettingsPropertyValueCollection.cs
- DesignerCategoryAttribute.cs
- Stylesheet.cs
- XmlSchemaFacet.cs
- ValidatingReaderNodeData.cs
- AutomationPeer.cs
- AssemblyInfo.cs
- ReflectionPermission.cs
- DataService.cs
- Logging.cs
- OrderingInfo.cs
- HMAC.cs
- JsonDataContract.cs
- ImageFormat.cs
- CryptoKeySecurity.cs
- TextBoxView.cs
- XmlReflectionImporter.cs
- LogicalExpr.cs
- RawUIStateInputReport.cs
- ObjectStateFormatter.cs
- COM2IDispatchConverter.cs
- ToolStripLocationCancelEventArgs.cs
- DataServiceProcessingPipelineEventArgs.cs
- TypeEnumerableViewSchema.cs