dotnet/wpf · error · InvalidOperationException

SR.MarkupExtensionResourceKey

Error message

SR.MarkupExtensionResourceKey

What it means

DynamicResourceExtension.ProvideValue is the markup-extension entry point for {DynamicResource} in XAML. It throws InvalidOperationException with MarkupExtensionResourceKey when the extension's ResourceKey property is null, because a dynamic resource reference is meaningless without a key.

Solutions

  1. Set ResourceKey in XAML: <DynamicResource ResourceKey="MyBrush"/> or {DynamicResource MyBrush}
  2. In code, use the DynamicResourceExtension(object resourceKey) constructor instead of the parameterless one
  3. Check generated XAML/templates for empty DynamicResource elements

Example fix

// before
var ext = new DynamicResourceExtension();
var val = ext.ProvideValue(sp); // throws
// after
var ext = new DynamicResourceExtension("MyBrush");
var val = ext.ProvideValue(sp);
Defensive patterns

Strategy: validation

Validate before calling

if (ext == null || ext.ResourceKey == null)
    throw new InvalidOperationException("DynamicResourceExtension requires a ResourceKey");

Type guard

bool HasResourceKey(DynamicResourceExtension ext) => ext?.ResourceKey != null;

Try / catch

try { value = ext.ProvideValue(sp); }
catch (InvalidOperationException ex) when (ex.Message.Contains("ResourceKey")) { value = DependencyProperty.UnsetValue; }

Prevention

When it happens

Trigger: Calling ProvideValue on a DynamicResourceExtension whose ResourceKey was never set (e.g. <DynamicResource/> with no key), or invoking ProvideValue programmatically after constructing DynamicResourceExtension() with the parameterless constructor.

Common situations: XAML authoring mistakes (missing ResourceKey attribute), code that builds DynamicResourceExtension instances manually for Style setters, data-binding or template generation tools emitting empty extensions.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DynamicResourceExtension.cs:55

        {
            ArgumentNullException.ThrowIfNull(resourceKey);
            _resourceKey = resourceKey;
        }


        /// <summary>
        ///  Return an object that should be set on the targetObject's targetProperty
        ///  for this markup extension.  For DynamicResourceExtension, this is the object found in
        ///  a resource dictionary in the current parent chain that is keyed by ResourceKey
        /// </summary>
        /// <returns>
        ///  The object to set on this property.
        /// </returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (ResourceKey == null)
            {
                throw new InvalidOperationException(SR.MarkupExtensionResourceKey);
            }

            if (serviceProvider != null)
            {
                // DynamicResourceExtensions are not allowed On CLR props except for Setter,Trigger,Condition (bugs 1183373,1572537)

                DependencyObject targetDependencyObject;
                DependencyProperty targetDependencyProperty;
                Helper.CheckCanReceiveMarkupExtension(this, serviceProvider, out targetDependencyObject, out targetDependencyProperty);
            }

            return new ResourceReferenceExpression(ResourceKey);
        }



        /// <summary>
        ///  The key in a Resource Dictionary used to find the object refered to by this

View on GitHub (pinned to 81131a70a4)