dotnet/wpf · error · ArgumentException
SR.NameScopeInvalidIdentifierName
Error message
SR.NameScopeInvalidIdentifierName
What it means
NameScope.RegisterName throws ArgumentException(SR.NameScopeInvalidIdentifierName) when the name is not a valid identifier per NameValidationHelper (e.g. contains spaces, starts with a digit, or has invalid characters). XAML names must be legal identifiers to be referenced in markup and generated code.
Solutions
- Sanitize the name to a valid identifier (letters/digits/underscore, not starting with a digit) before registering.
- Use a helper that generates safe names (e.g. prefix + counter).
- Validate with the same rules the library uses before calling RegisterName.
Example fix
// before scope.RegisterName(userInput, element); // after string safe = System.Text.RegularExpressions.Regex.Replace(userInput, "[^A-Za-z0-9_]", "_"); if (safe.Length > 0 && char.IsDigit(safe[0])) safe = "_" + safe; scope.RegisterName(safe, element);
Defensive patterns
Strategy: validation
Validate before calling
static bool IsValidXamlName(string n) => !string.IsNullOrEmpty(n) && !char.IsDigit(n[0]) && n.All(c => char.IsLetterOrDigit(c) || c == '_');
Type guard
bool IsSafeName(string n) => !string.IsNullOrEmpty(n) && System.Text.RegularExpressions.Regex.IsMatch(n, "^[A-Za-z_][A-Za-z0-9_]*$");
Try / catch
try { nameScope.RegisterName(name, element); } catch (ArgumentException ex) when (ex.Message.Contains("identifier")) { /* sanitize and retry */ } Prevention
- Sanitize names with a regex whitelist before registration
- Never use display text or file names directly as XAML names
When it happens
Trigger: Calling RegisterName with names like "my name", "1st", "a-b"; any name failing NameValidationHelper.IsValidIdentifierName.
Common situations: Generating names from user input or file names, localizing names with spaces, converting element titles directly into names.
Understand the failure class
Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.
Related errors
- SR.NameScopeInvalidIdentifierName
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.NameNotFound, propertyValue.ChildName)
- SR.Format(SR.NameScopeDuplicateNamesNotAllowed, childName)
- SR.Format(SR.NameScopeNotFound, name, "register")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/12d838d69899fbd6.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScope.cs:38
{
/// <summary>
/// Register Name-Object Map
/// </summary>
/// <param name="name">name to be registered</param>
/// <param name="scopedElement">object mapped to name</param>
public void RegisterName(string name, object scopedElement)
{
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(scopedElement);
if (name.Length == 0)
{
throw new ArgumentException(SR.NameScopeNameNotEmptyString);
}
if (!NameValidationHelper.IsValidIdentifierName(name))
{
throw new ArgumentException(SR.Format(SR.NameScopeInvalidIdentifierName, name));
}
if (_nameMap is null)
{
_nameMap = new HybridDictionary();
_nameMap[name] = scopedElement;
}
else
{
object nameContext = _nameMap[name];
// first time adding the Name, set it
if (nameContext is null)
{
_nameMap[name] = scopedElement;
}
else if (scopedElement != nameContext)
{
throw new ArgumentException(SR.Format(SR.NameScopeDuplicateNamesNotAllowed, name));View on GitHub (pinned to 81131a70a4)