Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Framework / System / Windows / Controls / DataErrorValidationRule.cs / 1 / 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.CommittedValue, 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 IsCriticalException() handles these correctly. #pragma warning disable 56500 string error; try { error = idei[name]; } catch (Exception ex) { if (CriticalExceptions.IsCriticalException(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. //---------------------------------------------------------------------------- // //// 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.CommittedValue, 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 IsCriticalException() handles these correctly. #pragma warning disable 56500 string error; try { error = idei[name]; } catch (Exception ex) { if (CriticalExceptions.IsCriticalException(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
- EventKeyword.cs
- OdbcConnectionFactory.cs
- GPRECT.cs
- DbMetaDataCollectionNames.cs
- AnimationClockResource.cs
- GacUtil.cs
- RenderingBiasValidation.cs
- ProviderUtil.cs
- ListControl.cs
- ASCIIEncoding.cs
- BlurBitmapEffect.cs
- SecurityCriticalDataForSet.cs
- QueryCacheKey.cs
- Visitor.cs
- CanonicalFontFamilyReference.cs
- SiteMapPath.cs
- DecoderNLS.cs
- OrderingQueryOperator.cs
- Panel.cs
- UIPropertyMetadata.cs
- RemoteWebConfigurationHostStream.cs
- ViewManagerAttribute.cs
- DefinitionBase.cs
- FtpWebResponse.cs
- HostedTransportConfigurationManager.cs
- GACIdentityPermission.cs
- HttpProtocolImporter.cs
- DiscoveryClientBindingElement.cs
- ResourcePermissionBase.cs
- ScrollChrome.cs
- SoundPlayer.cs
- SimpleLine.cs
- Rect.cs
- XmlSchemaSimpleContent.cs
- QueryModel.cs
- PageThemeCodeDomTreeGenerator.cs
- CodeTypeReference.cs
- ExtensionDataObject.cs
- NavigationProperty.cs
- CombinedGeometry.cs
- Int16AnimationBase.cs
- COM2PictureConverter.cs
- MatrixAnimationBase.cs
- UnSafeCharBuffer.cs
- UriScheme.cs
- PocoEntityKeyStrategy.cs
- LayoutEngine.cs
- EmbeddedMailObjectCollectionEditor.cs
- EditCommandColumn.cs
- DirectoryObjectSecurity.cs
- GenerateTemporaryTargetAssembly.cs
- JsonSerializer.cs
- IItemContainerGenerator.cs
- RequiredFieldValidator.cs
- TypedElement.cs
- ExceptionUtility.cs
- XamlSerializationHelper.cs
- ControlFilterExpression.cs
- WmpBitmapEncoder.cs
- PackWebRequestFactory.cs
- path.cs
- FocusWithinProperty.cs
- TextBoxBaseDesigner.cs
- PageParserFilter.cs
- DataServiceBehavior.cs
- RegexMatch.cs
- PasswordDeriveBytes.cs
- StrokeSerializer.cs
- StylusCaptureWithinProperty.cs
- AppearanceEditorPart.cs
- QilGeneratorEnv.cs
- OperationGenerator.cs
- TemplateControl.cs
- Point.cs
- EntitySqlQueryState.cs
- AutomationProperty.cs
- HttpDebugHandler.cs
- TextProperties.cs
- BufferedWebEventProvider.cs
- CodeDomConfigurationHandler.cs
- CompatibleComparer.cs
- HtmlControlPersistable.cs
- WpfSharedBamlSchemaContext.cs
- ServiceEndpointAssociationProvider.cs
- DBCommandBuilder.cs
- Run.cs
- TreeNodeBinding.cs
- ManagementOperationWatcher.cs
- RoleBoolean.cs
- SoapSchemaMember.cs
- TranslateTransform.cs
- TextInfo.cs
- WindowsButton.cs
- WebBrowserPermission.cs
- WorkflowItemPresenter.cs
- TabControlCancelEvent.cs
- TailPinnedEventArgs.cs
- VideoDrawing.cs
- DefaultTextStore.cs
- SizeChangedInfo.cs