dotnet/wpf · error · InvalidOperationException
SR.Format(SR.MissingContentSource, contentSource…
Error message
SR.Format(SR.MissingContentSource, contentSource, targetType)
What it means
TemplateContent.SetPropertyValMap-style code resolves properties named '{contentSource}', '{contentSource}Template', etc. on the target type via DependencyProperty.FromName. When the base content property (e.g. 'Content') does not exist on the target type even though a content source was specified, it throws InvalidOperationException (SR.MissingContentSource).
Solutions
- Ensure the templated control actually defines the content property referenced by ContentSource (e.g. inherit from ContentControl or define 'Content' DP).
- Correct or remove the ContentSource attribute in the template.
- Verify the template's TargetType matches the control being templated.
Example fix
// before <ControlTemplate TargetType="ItemsControl" ... ContentSource="Content"> <!-- no Content DP --> // after <ControlTemplate TargetType="ContentControl" ... ContentSource="Content">
Defensive patterns
Strategy: validation
Validate before calling
bool hasContent = typeof(ContentControl).IsAssignableFrom(targetType) ||
targetType.GetProperties().Any(p => p.Name == contentSource && typeof(DependencyProperty).IsAssignableFrom(AssociatedDP(p))); Type guard
static bool SupportsContentSource(Type t, string source) => DependencyProperty.FromName(source, t) != null;
Try / catch
try { element.Template = template; }
catch (InvalidOperationException ex) { log(ex.Message); applyFallbackTemplate(element); } Prevention
- Only set ContentSource on controls that define the matching DependencyProperty
- Verify TargetType before reusing templates across controls
- Spell ContentSource exactly as the DP name
When it happens
Trigger: Using the ContentSource attribute in a template whose TargetType does not define the named content property — DependencyProperty.FromName returns null for the base property while isContentSourceSet is true.
Common situations: Writing a ControlTemplate for a control that lacks the expected Content/ContentTemplate/ContentStringFormat family, or a typo in the ContentSource value, or targeting the wrong type with a template built for ContentControl.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- SR.Format(SR.TemplateMustBeFE, new object[] )
- SR.MarkupExtensionProperty (TemplateBindingExtension…
- SR.ParserUnexpectedEndEle (unexpected end of element while…
- Animation_ChildMustBeKeyFrame
- Animation_ChildMustBeKeyFrame
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c42a49b3149dfdfd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs:1236
bool isContentStringFormatPropertyDefined
)
{
if (String.IsNullOrEmpty(contentSource) && !isContentSourceSet)
contentSource = "Content";
if (!String.IsNullOrEmpty(contentSource) && !isContentPropertyDefined)
{
Debug.Assert(templateChildName != null);
DependencyProperty dpContent = DependencyProperty.FromName(contentSource, targetType);
DependencyProperty dpContentTemplate = DependencyProperty.FromName($"{contentSource}Template", targetType);
DependencyProperty dpContentTemplateSelector = DependencyProperty.FromName(
$"{contentSource}TemplateSelector", targetType);
DependencyProperty dpContentStringFormat = DependencyProperty.FromName($"{contentSource}StringFormat", targetType);
if (dpContent == null && isContentSourceSet)
{
throw new InvalidOperationException(SR.Format(SR.MissingContentSource, contentSource, targetType));
}
if (dpContent != null)
{
PropertyValue pv = new PropertyValue
{
ValueType = PropertyValueType.TemplateBinding,
ChildName = templateChildName,
ValueInternal = new TemplateBindingExtension(dpContent),
Property = ContentPresenter.ContentProperty
};
StyleHelper.UpdateTables(ref pv, ref childRecordFromChildIndex,
ref triggerSourceRecordFromChildIndex,
ref resourceDependents,
ref dataTriggerRecordFromBinding,
childIndexFromChildName,
ref hasInstanceValues);View on GitHub (pinned to 81131a70a4)