{"record":{"id":"249ffdeae0ec9cb1","repo":"dotnet/wpf","slug":"sr-markupextensionresourcekey","errorCode":null,"errorMessage":"SR.MarkupExtensionResourceKey","messagePattern":"SR\\.MarkupExtensionResourceKey","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DynamicResourceExtension.cs","lineNumber":55,"sourceCode":"        {\n            ArgumentNullException.ThrowIfNull(resourceKey);\n            _resourceKey = resourceKey;\n        }\n\n\n        /// <summary>\n        ///  Return an object that should be set on the targetObject's targetProperty\n        ///  for this markup extension.  For DynamicResourceExtension, this is the object found in\n        ///  a resource dictionary in the current parent chain that is keyed by ResourceKey\n        /// </summary>\n        /// <returns>\n        ///  The object to set on this property.\n        /// </returns>\n        public override object ProvideValue(IServiceProvider serviceProvider)\n        {\n            if (ResourceKey == null)\n            {\n                throw new InvalidOperationException(SR.MarkupExtensionResourceKey);\n            }\n\n            if (serviceProvider != null)\n            {\n                // DynamicResourceExtensions are not allowed On CLR props except for Setter,Trigger,Condition (bugs 1183373,1572537)\n\n                DependencyObject targetDependencyObject;\n                DependencyProperty targetDependencyProperty;\n                Helper.CheckCanReceiveMarkupExtension(this, serviceProvider, out targetDependencyObject, out targetDependencyProperty);\n            }\n\n            return new ResourceReferenceExpression(ResourceKey);\n        }\n\n\n\n        /// <summary>\n        ///  The key in a Resource Dictionary used to find the object refered to by this","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DynamicResourceExtension.cs#L37-L73","documentation":"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.","triggerScenarios":"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.","commonSituations":"XAML authoring mistakes (missing ResourceKey attribute), code that builds DynamicResourceExtension instances manually for Style setters, data-binding or template generation tools emitting empty extensions.","solutions":["Set ResourceKey in XAML: <DynamicResource ResourceKey=\"MyBrush\"/> or {DynamicResource MyBrush}","In code, use the DynamicResourceExtension(object resourceKey) constructor instead of the parameterless one","Check generated XAML/templates for empty DynamicResource elements"],"exampleFix":"// before\nvar ext = new DynamicResourceExtension();\nvar val = ext.ProvideValue(sp); // throws\n// after\nvar ext = new DynamicResourceExtension(\"MyBrush\");\nvar val = ext.ProvideValue(sp);","handlingStrategy":"validation","validationCode":"if (ext == null || ext.ResourceKey == null)\n    throw new InvalidOperationException(\"DynamicResourceExtension requires a ResourceKey\");","typeGuard":"bool HasResourceKey(DynamicResourceExtension ext) => ext?.ResourceKey != null;","tryCatchPattern":"try { value = ext.ProvideValue(sp); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ResourceKey\")) { value = DependencyProperty.UnsetValue; }","preventionTips":["Always supply ResourceKey in XAML or use the object constructor","Lint generated XAML for empty <DynamicResource/> elements"],"tags":["wpf","xaml","dynamicresource","markup-extension"],"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"}