{"record":{"id":"d0386a9d3bd273cf","repo":"dotnet/wpf","slug":"sr-namesnotsupportedinsideresourcedictionary","errorCode":null,"errorMessage":"SR.NamesNotSupportedInsideResourceDictionary","messagePattern":"SR\\.NamesNotSupportedInsideResourceDictionary","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs","lineNumber":284,"sourceCode":"                ResourceDictionaryDiagnostics.AddResourceDictionaryForUri(uri, this);\n\n                if (!IsInitializePending)\n                {\n                    // Fire Invalidations for the changes made by asigning a new Source\n                    NotifyOwners(new ResourcesChangeInfo(null, this));\n                }\n            }\n        }\n\n        #region INameScope\n        /// <summary>\n        /// Registers the name - element combination\n        /// </summary>\n        /// <param name=\"name\">name of the element</param>\n        /// <param name=\"scopedElement\">Element where name is defined</param>\n        public void RegisterName(string name, object scopedElement)\n        {\n            throw new NotSupportedException(SR.NamesNotSupportedInsideResourceDictionary);\n        }\n\n        /// <summary>\n        /// Unregisters the name - element combination\n        /// </summary>\n        /// <param name=\"name\">Name of the element</param>\n        public void UnregisterName(string name)\n        {\n            // Do Nothing as Names cannot be registered on ResourceDictionary\n        }\n\n        /// <summary>\n        /// Find the element given name\n        /// </summary>\n        /// <param name=\"name\">Name of the element</param>\n        /// <returns>null always</returns>\n        public object FindName(string name)\n        {","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs#L266-L302","documentation":"ResourceDictionary implements INameScope but explicitly does not support name registration; RegisterName always throws NotSupportedException (SR.NamesNotSupportedInsideResourceDictionary). Names (x:Name) are not allowed inside resource dictionaries.","triggerScenarios":"Calling INameScope.RegisterName on a ResourceDictionary, or XAML BAML/parse processing that encounters x:Name inside a ResourceDictionary and routes the registration to it.","commonSituations":"Declaring x:Name on an element inside a ResourceDictionary in XAML; framework code that programmatically registers names against any INameScope without checking the type.","solutions":["Remove x:Name from elements inside ResourceDictionary entries; use x:Key instead for dictionary resources.","If the element needs a name, move it out of the ResourceDictionary into the visual tree or a template with its own namescope.","In programmatic code, check for ResourceDictionary before calling RegisterName on an INameScope."],"exampleFix":"// before (XAML inside ResourceDictionary)\n<ResourceDictionary><Button x:Name=\"okButton\"/></ResourceDictionary>\n// after\n<ResourceDictionary><Button x:Key=\"okButton\"/></ResourceDictionary>","handlingStrategy":"type-guard","validationCode":"if (nameScope is ResourceDictionary) throw new NotSupportedException(\"ResourceDictionary does not support name registration; use x:Key for dictionary resources\");","typeGuard":"bool SupportsNameRegistration(INameScope scope) => scope is not ResourceDictionary;","tryCatchPattern":"try { nameScope.RegisterName(name, element); } catch (NotSupportedException ex) when (ex.Message.Contains(\"names\")) { /* fall back to x:Key semantics or skip */ }","preventionTips":["Never use x:Name inside a ResourceDictionary; use x:Key","Move named elements into ControlTemplates/DataTemplates where namescopes are supported","Before calling RegisterName on an INameScope, check it is not a ResourceDictionary"],"tags":["wpf","resourcedictionary","xaml","namescope"],"backgroundTag":"unsupported-operation","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"}