dotnet/wpf · error · InvalidOperationException
SR.ObjectDataProviderCanHaveOnlyOneSource
Error message
SR.ObjectDataProviderCanHaveOnlyOneSource
What it means
ObjectDataProvider allows exactly one source for the object: either ObjectType (a type to construct) or ObjectInstance (a live instance). Setting ObjectType while the provider is already in instance mode (SourceMode.FromInstance) throws InvalidOperationException because both sources cannot coexist. To switch modes you must first null out the other property.
Solutions
- Set ObjectInstance = null before assigning ObjectType
- Or set ObjectType = null before assigning ObjectInstance
- Restructure so a fresh ObjectDataProvider is created per mode instead of mutating one
Example fix
// before provider.ObjectInstance = new MyService(); provider.ObjectType = typeof(MyService); // throws // after provider.ObjectInstance = null; provider.ObjectType = typeof(MyService);
Defensive patterns
Strategy: validation
Validate before calling
if (provider.ObjectInstance != null)
provider.ObjectInstance = null;
provider.ObjectType = typeof(MyService); Prevention
- Null the other property before switching ObjectType/ObjectInstance
- Prefer declaring the source kind once in XAML
- Wrap mode switches in a helper that clears both first
When it happens
Trigger: Setting ObjectDataProvider.ObjectType when ObjectInstance was previously set (and not nulled first) — the internal _mode is SourceMode.FromInstance.
Common situations: XAML or code where a developer switches an ObjectDataProvider from a declared instance to a type (e.g. for design-time to runtime swaps) without clearing ObjectInstance; hand-edited XAML that sets both x:Name instance wiring and ObjectType.
Related errors
- ArgumentNullException(nameof(value))
- (no message) NotSupportedException
- NotImplementedException (ConvertBack not supported)
- NotImplementedException (ConvertBack not supported)
- NotSupportedException (ConvertBack not supported)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9f8a8ec1971da2de.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ObjectDataProvider.cs:98
/// <summary>
/// The type to instantiate for use as ObjectInstance.
/// This is null when the ObjectDataProvider is uninitialized or explicitly set to null.
/// If the user makes an assignment to ObjectInstance, ObjectType will return
/// the Type of the object (or null if the object is null).
/// </summary>
/// <remarks>
/// Only one of ObjectType or ObjectInstance can be set by the user to a non-null value.
/// While refresh is deferred, ObjectInstance and Data will not update until Refresh() is called.
/// </remarks>
public Type ObjectType
{
get { return _objectType; }
set
{
// User is only allowed to set one of ObjectType or ObjectInstance.
// To change "mode", the user must null the other property first.
if (_mode == SourceMode.FromInstance)
throw new InvalidOperationException(SR.ObjectDataProviderCanHaveOnlyOneSource);
_mode = (value == null) ? SourceMode.NoSource : SourceMode.FromType;
_constructorParameters.SetReadOnly(false);
if (_needNewInstance = SetObjectType(value)) // raises property changed event
{
// Note: ObjectInstance and Data are not updated until Refresh() happens!
if (! IsRefreshDeferred)
Refresh();
}
}
}
/// <summary>
/// This method is used by TypeDescriptor to determine if this property should
/// be serialized.
/// </summary>View on GitHub (pinned to 81131a70a4)