dotnet/wpf · error · InvalidOperationException
SR.ValidationRule_UnexpectedValue
Error message
SR.ValidationRule_UnexpectedValue
What it means
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.
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.
Example fix
// before binding.ValidationRules.Add(DataErrorValidationRule.Instance); // wrong place // after binding.ValidatesOnDataErrors = true; // or add rule to BindingGroup.ValidationRules
Defensive patterns
Strategy: validation
Validate before calling
// pass a BindingGroup to Validate, or attach the rule at the right level bool isBindingGroupValue = value is BindingGroup;
Type guard
static bool CanRunDataErrorRule(object value) => value is System.Windows.Data.BindingGroup;
Try / catch
try
{
var result = DataErrorValidationRule.Instance.Validate(value, culture);
}
catch (InvalidOperationException)
{
// rule got a per-property value; use ValidatesOnDataErrors on the Binding instead
} Prevention
- Use ValidatesOnDataErrors=true on Bindings for per-property IDataErrorInfo
- Put DataErrorValidationRule only in BindingGroup.ValidationRules
- Never call Validate manually with a raw property value
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Bracket characters cannot be alpha-numeric or whitespace.
- Bracket characters cannot be one of the following: '=' …
- Cannot have empty collection of DocumentPageView objects.
- index
- InvalidDrawingAttributesWidth
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/78cc750fcc124ac8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataErrorValidationRule.cs:105
{
TraceData.TraceAndNotify(TraceEventType.Error,
TraceData.DataErrorInfoFailed(
name,
idei.GetType().FullName,
ex.GetType().FullName,
ex.Message),
bindingExpr);
}
}
if (!String.IsNullOrEmpty(error))
{
return new ValidationResult(false, error);
}
}
}
else
throw new InvalidOperationException(SR.Format(SR.ValidationRule_UnexpectedValue, this, value));
return ValidationResult.ValidResult;
}
internal static readonly DataErrorValidationRule Instance = new DataErrorValidationRule();
}
}
View on GitHub (pinned to 81131a70a4)