dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MissingContentSource, prefix, targetType)

Error message

SR.Format(SR.MissingContentSource, prefix, targetType)

What it means

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.

Solutions

  1. Add the missing dependency property (e.g. ContentProperty or '<prefix>Content') to the target type via DependencyProperty.Register.
  2. Use a target type that already defines the expected content source property (e.g. derive from ContentControl/HeaderedContentControl).
  3. Set the template's target properties explicitly instead of relying on auto-alias, and avoid triggering the alias path.

Example fix

// before
class MyHeaderControl : Control { } // no Header DP; template auto-alias fails
// after
class MyHeaderControl : HeaderedContentControl { } // provides Header source property
Defensive patterns

Strategy: validation

Validate before calling

if (DependencyProperty.FromName("Header", targetType) == null)
    throw new InvalidOperationException($"{targetType.Name} lacks the content source property required for auto-alias");

Type guard

bool HasContentSource(Type t, string prefix) => DependencyProperty.FromName($"{prefix}Content", t) != null;

Try / catch

try { template.Seal(); } catch (InvalidOperationException ex) when (ex.Message.Contains("content source") || ex.Message.Contains("MissingContentSource")) { /* add the DP or change target type */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/0276fef3bced0698. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs:1183

                object o = GetValue(ContentPresenter.ContentSourceProperty);
                string prefix = (o == DependencyProperty.UnsetValue) ? "Content" : (string)o;

                // if Content is previously set, do nothing
                if (!String.IsNullOrEmpty(prefix) && !IsValueDefined(ContentPresenter.ContentProperty))
                {
                    // find source properties, using prefix
                    Debug.Assert(_frameworkTemplate != null, "ContentPresenter is an FE and can only have a FrameworkTemplate");
                    Type targetType = _frameworkTemplate.TargetTypeInternal;

                    DependencyProperty dpContent = DependencyProperty.FromName(prefix, targetType);
                    DependencyProperty dpContentTemplate = DependencyProperty.FromName($"{prefix}Template", targetType);
                    DependencyProperty dpContentTemplateSelector = DependencyProperty.FromName($"{prefix}TemplateSelector", targetType);
                    DependencyProperty dpContentStringFormat = DependencyProperty.FromName($"{prefix}StringFormat", targetType);

                    // if desired source for Content doesn't exist, report an error
                    if (dpContent == null && o != DependencyProperty.UnsetValue)
                    {
                        throw new InvalidOperationException(SR.Format(SR.MissingContentSource, prefix, targetType));
                    }

                    // auto-alias the Content property
                    if (dpContent != null)
                    {
                        SetValue(ContentPresenter.ContentProperty, new TemplateBindingExtension(dpContent));
                    }

                    // auto-alias the remaining properties if none of them are previously set
                    if (!IsValueDefined(ContentPresenter.ContentTemplateProperty) &&
                        !IsValueDefined(ContentPresenter.ContentTemplateSelectorProperty) &&
                        !IsValueDefined(ContentPresenter.ContentStringFormatProperty))
                    {
                        if (dpContentTemplate != null)
                            SetValue(ContentPresenter.ContentTemplateProperty, new TemplateBindingExtension(dpContentTemplate));
                        if (dpContentTemplateSelector != null)
                            SetValue(ContentPresenter.ContentTemplateSelectorProperty, new TemplateBindingExtension(dpContentTemplateSelector));
                        if (dpContentStringFormat != null)

View on GitHub (pinned to 81131a70a4)