{"record":{"id":"0f39d910cb5f8e08","repo":"dotnet/wpf","slug":"sr-namescopeinvalididentifiername-namescopedictionary","errorCode":null,"errorMessage":"SR.NameScopeInvalidIdentifierName","messagePattern":"SR\\.NameScopeInvalidIdentifierName","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs","lineNumber":47,"sourceCode":"\n        public NameScopeDictionary(INameScope underlyingNameScope)\n        {\n            _names = new FrugalObjectList<string>();\n            _underlyingNameScope = underlyingNameScope ?? throw new ArgumentNullException(nameof(underlyingNameScope));\n        }\n\n        public void RegisterName(string name, object scopedElement)\n        {\n            ArgumentNullException.ThrowIfNull(name);\n\n            ArgumentNullException.ThrowIfNull(scopedElement);\n\n            if (name.Length == 0)\n                throw new ArgumentException(SR.NameScopeNameNotEmptyString);\n\n            if (!NameValidationHelper.IsValidIdentifierName(name))\n            {\n                throw new ArgumentException(SR.Format(SR.NameScopeInvalidIdentifierName, name));\n            }\n\n            if (_underlyingNameScope is not null)\n            {\n                _names.Add(name);\n                _underlyingNameScope.RegisterName(name, scopedElement);\n            }\n            else\n            {\n                if (_nameMap is null)\n                {\n                    _nameMap = new HybridDictionary();\n                    _nameMap[name] = scopedElement;\n                }\n                else\n                {\n                    object nameContext = _nameMap[name];\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs#L29-L65","documentation":"RegisterName validates the name with NameValidationHelper.IsValidIdentifierName; if the string is not a valid XAML identifier (e.g. contains spaces, punctuation, or starts with a digit), it throws ArgumentException with SR.NameScopeInvalidIdentifierName including the offending name.","triggerScenarios":"Calling RegisterName with names containing invalid identifier characters — spaces, dots, hyphens, leading digits, symbols.","commonSituations":"Using element type names like \"System.Windows.Controls.Button\" or file names as x:Name values; names auto-derived from paths or user input; localized strings used as names.","solutions":["Sanitize the name to contain only valid identifier characters before registering","Replace invalid characters programmatically (e.g. strip non-alphanumeric characters)","Generate a synthetic valid name instead of using display text"],"exampleFix":"// before\nnameScopeDictionary.RegisterName(\"My Control 1\", element);\n// after\nstring validName = Regex.Replace(\"My Control 1\", @\"[^A-Za-z0-9_]\", \"\");\nnameScopeDictionary.RegisterName(validName, element);","handlingStrategy":"validation","validationCode":"static readonly Regex ValidName = new(@\"^[A-Za-z_][A-Za-z0-9_]*$\");\nif (!ValidName.IsMatch(name)) name = Sanitize(name);\nnameScopeDictionary.RegisterName(name, element);","typeGuard":"static bool IsIdentifierName(string name) => !string.IsNullOrEmpty(name) && NameValidationHelper.IsValidIdentifierName(name);","tryCatchPattern":"try { nameScopeDictionary.RegisterName(name, element); }\ncatch (ArgumentException ex) { /* name not a valid identifier: ex.Message includes name */ }","preventionTips":["Sanitize names generated from display text, paths, or type names","Never use strings with spaces or dots as x:Name","Test generated names against identifier rules"],"tags":["wpf","system-xaml","namescope","validation","identifier"],"backgroundTag":"invalid-identifier-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}