{"record":{"id":"fbc7ec581a4e800c","repo":"dotnet/wpf","slug":"sr-namescopenamenotemptystring-namescope","errorCode":null,"errorMessage":"SR.NameScopeNameNotEmptyString","messagePattern":"SR\\.NameScopeNameNotEmptyString","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/NameScope.cs","lineNumber":42,"sourceCode":"    /// within the logical tree section.\n    /// </summary>\n    [TypeForwardedFrom(\"PresentationFramework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\")]\n    public class NameScope : INameScopeDictionary\n    {        \n        #region INameScope\n        \n        /// <summary>\n        /// Register Name-Object Map \n        /// </summary>\n        /// <param name=\"name\">name to be registered</param>\n        /// <param name=\"scopedElement\">object mapped to name</param>\n        public void RegisterName(string name, object scopedElement)\n        {\n            ArgumentNullException.ThrowIfNull(name);\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 (_nameMap == null)\n            {\n                _nameMap = new HybridDictionary();\n                _nameMap[name] = scopedElement;\n            }\n            else\n            {\n                object nameContext = _nameMap[name];\n                // first time adding the Name, set it\n                if (nameContext == null)\n                {\n                    _nameMap[name] = scopedElement;","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/NameScope.cs#L24-L60","documentation":"NameScope.RegisterName throws ArgumentException(SR.NameScopeNameNotEmptyString) when the name argument is the empty string. XAML names must be non-empty valid identifiers. The guard runs after the null check and before identifier validation.","triggerScenarios":"Calling RegisterName(\"\", element) on a NameScope or via INameScope; binding a Name property from data where the value resolved to empty.","commonSituations":"Data-bound names where the source field is empty; string manipulation that stripped the name; deserializing XAML with a missing x:Name value.","solutions":["Validate string.IsNullOrEmpty(name) before calling RegisterName and reject early with a meaningful message.","Ensure the data source supplying the name provides a non-empty value.","Generate a fallback name (e.g. \"element1\") when the supplied name is empty.","Catch ArgumentException and report the invalid name to the caller."],"exampleFix":"// before\nscope.RegisterName(name, element); // name may be \"\"\n// after\nif (!string.IsNullOrEmpty(name))\n    scope.RegisterName(name, element);","handlingStrategy":"validation","validationCode":"bool valid = !string.IsNullOrEmpty(name);","typeGuard":"static bool IsValidScopeName(string name) => !string.IsNullOrEmpty(name);","tryCatchPattern":"try { scope.RegisterName(name, element); }\ncatch (ArgumentException ex) { /* name was empty or invalid */ }","preventionTips":["Validate names at the data-source boundary before they reach RegisterName.","Never bind RegisterName's name argument to possibly-empty data without a check.","Generate fallback names for empty inputs."],"tags":["wpf","xaml","namescope","argument-validation"],"backgroundTag":"empty-required-field","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"}