{"record":{"id":"78cc750fcc124ac8","repo":"dotnet/wpf","slug":"sr-validationrule-unexpectedvalue","errorCode":null,"errorMessage":"SR.ValidationRule_UnexpectedValue","messagePattern":"SR\\.ValidationRule_UnexpectedValue","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataErrorValidationRule.cs","lineNumber":105,"sourceCode":"                        {\n                            TraceData.TraceAndNotify(TraceEventType.Error,\n                                            TraceData.DataErrorInfoFailed(\n                                                name,\n                                                idei.GetType().FullName,\n                                                ex.GetType().FullName,\n                                                ex.Message),\n                                            bindingExpr);\n                        }\n                    }\n\n                    if (!String.IsNullOrEmpty(error))\n                    {\n                        return new ValidationResult(false, error);\n                    }\n                }\n            }\n            else\n                throw new InvalidOperationException(SR.Format(SR.ValidationRule_UnexpectedValue, this, value));\n\n            return ValidationResult.ValidResult;\n        }\n\n        internal static readonly DataErrorValidationRule Instance = new DataErrorValidationRule();\n    }\n}\n\n","sourceCodeStart":87,"sourceCodeEnd":114,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataErrorValidationRule.cs#L87-L114","documentation":"DataErrorValidationRule.Validate expects the value being validated to be a BindingGroup (it validates the whole item via IDataErrorInfo). If the value is anything else, it throws InvalidOperationException(SR.Format(SR.ValidationRule_UnexpectedValue, this, value)) because the rule cannot interpret it.","triggerScenarios":"Using DataErrorValidationRule (or ValidatesOnDataErrors) in a Binding whose ValidationRules run on an individual property value instead of a BindingGroup context — i.e., attaching the rule to a plain binding rather than a BindingGroup, or calling Validate directly with a non-BindingGroup value.","commonSituations":"Adding DataErrorValidationRule to a per-property Binding.ValidationRules instead of BindingGroup.ValidationRules, calling rule.Validate manually with a raw property value, or custom validation pipelines reusing the rule incorrectly.","solutions":["Add the DataErrorValidationRule to the BindingGroup's ValidationRules (or use ItemLevel validation via ValidatesOnDataErrors on the binding's data item context).","For per-property IDataErrorInfo validation, rely on ValidatesOnDataErrors=true on the Binding rather than manually adding the rule.","If calling Validate programmatically, pass the BindingGroup value the rule expects.","Verify the validation pipeline stage: this rule runs at UpdateSource/commit level, not per-keypress property conversion."],"exampleFix":"// before\nbinding.ValidationRules.Add(DataErrorValidationRule.Instance); // wrong place\n// after\nbinding.ValidatesOnDataErrors = true; // or add rule to BindingGroup.ValidationRules","handlingStrategy":"validation","validationCode":"// pass a BindingGroup to Validate, or attach the rule at the right level\nbool isBindingGroupValue = value is BindingGroup;","typeGuard":"static bool CanRunDataErrorRule(object value) => value is System.Windows.Data.BindingGroup;","tryCatchPattern":"try\n{\n    var result = DataErrorValidationRule.Instance.Validate(value, culture);\n}\ncatch (InvalidOperationException)\n{\n    // rule got a per-property value; use ValidatesOnDataErrors on the Binding instead\n}","preventionTips":["Use ValidatesOnDataErrors=true on Bindings for per-property IDataErrorInfo","Put DataErrorValidationRule only in BindingGroup.ValidationRules","Never call Validate manually with a raw property value"],"tags":["wpf","validation","databinding","idataerrorinfo"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}