{"record":{"id":"c42a49b3149dfdfd","repo":"dotnet/wpf","slug":"sr-format-sr-missingcontentsource-contentsource-targettype","errorCode":null,"errorMessage":"SR.Format(SR.MissingContentSource, contentSource, targetType)","messagePattern":"SR\\.Format\\(SR\\.MissingContentSource, contentSource, targetType\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs","lineNumber":1236,"sourceCode":"            bool isContentStringFormatPropertyDefined\n            )\n        {\n            if (String.IsNullOrEmpty(contentSource) && !isContentSourceSet)\n                contentSource = \"Content\";\n\n            if (!String.IsNullOrEmpty(contentSource) && !isContentPropertyDefined)\n            {\n                Debug.Assert(templateChildName != null);\n\n                DependencyProperty dpContent = DependencyProperty.FromName(contentSource, targetType);\n                DependencyProperty dpContentTemplate = DependencyProperty.FromName($\"{contentSource}Template\", targetType);\n                DependencyProperty dpContentTemplateSelector = DependencyProperty.FromName(\n                    $\"{contentSource}TemplateSelector\", targetType);\n                DependencyProperty dpContentStringFormat = DependencyProperty.FromName($\"{contentSource}StringFormat\", targetType);\n\n                if (dpContent == null && isContentSourceSet)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.MissingContentSource, contentSource, targetType));\n                }\n\n                if (dpContent != null)\n                {\n                    PropertyValue pv = new PropertyValue\n                    {\n                        ValueType = PropertyValueType.TemplateBinding,\n                        ChildName = templateChildName,\n                        ValueInternal = new TemplateBindingExtension(dpContent),\n                        Property = ContentPresenter.ContentProperty\n                    };\n\n                    StyleHelper.UpdateTables(ref pv, ref childRecordFromChildIndex,\n                        ref triggerSourceRecordFromChildIndex,\n                        ref resourceDependents,\n                        ref dataTriggerRecordFromBinding,\n                        childIndexFromChildName,\n                        ref hasInstanceValues);","sourceCodeStart":1218,"sourceCodeEnd":1254,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs#L1218-L1254","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n<ControlTemplate TargetType=\"ItemsControl\" ... ContentSource=\"Content\">  <!-- no Content DP -->\n// after\n<ControlTemplate TargetType=\"ContentControl\" ... ContentSource=\"Content\">","handlingStrategy":"validation","validationCode":"bool hasContent = typeof(ContentControl).IsAssignableFrom(targetType) ||\n    targetType.GetProperties().Any(p => p.Name == contentSource && typeof(DependencyProperty).IsAssignableFrom(AssociatedDP(p)));","typeGuard":"static bool SupportsContentSource(Type t, string source) => DependencyProperty.FromName(source, t) != null;","tryCatchPattern":"try { element.Template = template; }\ncatch (InvalidOperationException ex) { log(ex.Message); applyFallbackTemplate(element); }","preventionTips":["Only set ContentSource on controls that define the matching DependencyProperty","Verify TargetType before reusing templates across controls","Spell ContentSource exactly as the DP name"],"tags":["wpf","xaml","template","missing-property"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}