{"record":{"id":"f648f3e74798508e","repo":"dotnet/wpf","slug":"sr-themedictionaryextension-source","errorCode":null,"errorMessage":"SR.ThemeDictionaryExtension_Source","messagePattern":"SR\\.ThemeDictionaryExtension_Source","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeDictionaryExtension.cs","lineNumber":86,"sourceCode":"                throw new InvalidOperationException(SR.ThemeDictionaryExtension_Name);\n            }\n\n            IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;\n            if( provideValueTarget == null )\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, \"IProvideValueTarget\" ));\n            }\n                \n            object targetObject = provideValueTarget.TargetObject;\n            object targetProperty = provideValueTarget.TargetProperty;\n\n            ResourceDictionary dictionary = targetObject as ResourceDictionary;\n            PropertyInfo propertyInfo = targetProperty as PropertyInfo;\n\n            // Allow targetProperty to be null or ResourceDictionary.Source\n            if (dictionary == null || (targetProperty != null && propertyInfo != SourceProperty))\n            {\n                throw new InvalidOperationException(SR.ThemeDictionaryExtension_Source);\n            }\n\n            Register(dictionary, _assemblyName);\n            dictionary.IsSourcedFromThemeDictionary = true;\n\n            return GenerateUri(_assemblyName, SystemResources.ResourceDictionaries.ThemedResourceName, MS.Win32.UxThemeWrapper.ThemeName);\n        }\n\n        // Build the Uri for the assembly:\n        // /AssemblyName;Component/themes/<CurrentTheme>.<Color>.xaml\n        private static Uri GenerateUri(string assemblyName, string resourceName, ReadOnlySpan<char> themeName)\n        {\n            StringBuilder uri = new StringBuilder(assemblyName.Length + 50);\n\n            uri.Append('/');\n            uri.Append(assemblyName);\n\n            // If assembly is PresentationFramework, append the Theme name","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeDictionaryExtension.cs#L68-L104","documentation":"ThemeDictionaryExtension may only be applied to a ResourceDictionary, targeting either the dictionary itself or its Source property. If the target object is not a ResourceDictionary, or the target property is neither null nor the Source property, ProvideValue throws InvalidOperationException with SR.ThemeDictionaryExtension_Source.","triggerScenarios":"Placing {ThemeDictionary ...} on a property whose value is not a ResourceDictionary (e.g. a Brush, Uri, or Style property); applying it to a different property of a ResourceDictionary such as DeferredContent.","commonSituations":"Copy-paste of the markup extension into the wrong XAML attribute; misunderstanding that the extension must sit on <ResourceDictionary> or its Source attribute, not on arbitrary resource values.","solutions":["Use the extension only as: <ResourceDictionary Source=\"{ThemeDictionary AssemblyName}\"/> or wrapping a ResourceDictionary element.","Move the extension from the incorrect property to the ResourceDictionary Source.","If a plain theme URI is needed on another property, compute the pack URI in code instead."],"exampleFix":"// before\n<Setter Property=\"Background\" Value=\"{ThemeDictionary MyApp}\"/> <!-- throws -->\n// after\n<ResourceDictionary Source=\"{ThemeDictionary MyApp}\"/>","handlingStrategy":"validation","validationCode":"bool isOnDictionarySource = targetObject is ResourceDictionary &&\n    (targetProperty == null || targetProperty == ThemeDictionaryExtension_SourceProperty);\nif (!isOnDictionarySource) throw new InvalidOperationException(\"Place ThemeDictionary only on a ResourceDictionary's Source.\");","typeGuard":null,"tryCatchPattern":"try { return (Uri)ext.ProvideValue(sp); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Source\") || ex.Message.Contains(\"ResourceDictionary\")) { return null; }","preventionTips":["Only use {ThemeDictionary} as <ResourceDictionary Source=\"{ThemeDictionary X}\"/>.","Never paste the extension onto non-dictionary properties.","Review merged-dictionary XAML after copy/paste refactors."],"tags":["wpf","markup-extension","xaml"],"backgroundTag":"incompatible-source-type","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"}