{"record":{"id":"0276fef3bced0698","repo":"dotnet/wpf","slug":"sr-format-sr-missingcontentsource-prefix-targettype","errorCode":null,"errorMessage":"SR.Format(SR.MissingContentSource, prefix, targetType)","messagePattern":"SR\\.Format\\(SR\\.MissingContentSource, prefix, targetType\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":1183,"sourceCode":"                object o = GetValue(ContentPresenter.ContentSourceProperty);\n                string prefix = (o == DependencyProperty.UnsetValue) ? \"Content\" : (string)o;\n\n                // if Content is previously set, do nothing\n                if (!String.IsNullOrEmpty(prefix) && !IsValueDefined(ContentPresenter.ContentProperty))\n                {\n                    // find source properties, using prefix\n                    Debug.Assert(_frameworkTemplate != null, \"ContentPresenter is an FE and can only have a FrameworkTemplate\");\n                    Type targetType = _frameworkTemplate.TargetTypeInternal;\n\n                    DependencyProperty dpContent = DependencyProperty.FromName(prefix, targetType);\n                    DependencyProperty dpContentTemplate = DependencyProperty.FromName($\"{prefix}Template\", targetType);\n                    DependencyProperty dpContentTemplateSelector = DependencyProperty.FromName($\"{prefix}TemplateSelector\", targetType);\n                    DependencyProperty dpContentStringFormat = DependencyProperty.FromName($\"{prefix}StringFormat\", targetType);\n\n                    // if desired source for Content doesn't exist, report an error\n                    if (dpContent == null && o != DependencyProperty.UnsetValue)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.MissingContentSource, prefix, targetType));\n                    }\n\n                    // auto-alias the Content property\n                    if (dpContent != null)\n                    {\n                        SetValue(ContentPresenter.ContentProperty, new TemplateBindingExtension(dpContent));\n                    }\n\n                    // auto-alias the remaining properties if none of them are previously set\n                    if (!IsValueDefined(ContentPresenter.ContentTemplateProperty) &&\n                        !IsValueDefined(ContentPresenter.ContentTemplateSelectorProperty) &&\n                        !IsValueDefined(ContentPresenter.ContentStringFormatProperty))\n                    {\n                        if (dpContentTemplate != null)\n                            SetValue(ContentPresenter.ContentTemplateProperty, new TemplateBindingExtension(dpContentTemplate));\n                        if (dpContentTemplateSelector != null)\n                            SetValue(ContentPresenter.ContentTemplateSelectorProperty, new TemplateBindingExtension(dpContentTemplateSelector));\n                        if (dpContentStringFormat != null)","sourceCodeStart":1165,"sourceCodeEnd":1201,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L1165-L1201","documentation":"During Seal(), ApplyAutoAliasRules auto-aliases ContentPresenter's Content/ContentTemplate/etc. properties to matching properties on the template's target type (e.g. Header/Content properties with a given prefix). If the target type does not declare the expected content source property (e.g. 'Content' or '<prefix>Content') while the framework still expects to alias from it (o != UnsetValue), an InvalidOperationException naming the prefix and target type is thrown.","triggerScenarios":"Sealing a template whose TargetType lacks the required content source property that the auto-alias logic requires — e.g. building a HeaderedContentControl-like template where 'Header' content must alias from the target type but the type defines no 'Header' (or '<prefix>Content') dependency property.","commonSituations":"Programmatically creating ControlTemplates for custom controls that do not define the conventional Content/Header dependency properties; renaming or removing a Content-like property on a control while old template code still relies on auto-aliasing; mismatched prefix assumptions (Content vs Header) in template construction.","solutions":["Add the missing dependency property (e.g. ContentProperty or '<prefix>Content') to the target type via DependencyProperty.Register.","Use a target type that already defines the expected content source property (e.g. derive from ContentControl/HeaderedContentControl).","Set the template's target properties explicitly instead of relying on auto-alias, and avoid triggering the alias path."],"exampleFix":"// before\nclass MyHeaderControl : Control { } // no Header DP; template auto-alias fails\n// after\nclass MyHeaderControl : HeaderedContentControl { } // provides Header source property","handlingStrategy":"validation","validationCode":"if (DependencyProperty.FromName(\"Header\", targetType) == null)\n    throw new InvalidOperationException($\"{targetType.Name} lacks the content source property required for auto-alias\");","typeGuard":"bool HasContentSource(Type t, string prefix) => DependencyProperty.FromName($\"{prefix}Content\", t) != null;","tryCatchPattern":"try { template.Seal(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"content source\") || ex.Message.Contains(\"MissingContentSource\")) { /* add the DP or change target type */ }","preventionTips":["Derive template target types from ContentControl/HeaderedContentControl so content source properties exist","Register conventional Content/Header dependency properties on custom controls","Don't rename/remove content DPs that templates alias from"],"tags":["wpf","template","missing-dependency","property-system"],"backgroundTag":"missing-dependency","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"}