dotnet/wpf · error · ArgumentException

SR.NameScopeInvalidIdentifierName

Error message

SR.NameScopeInvalidIdentifierName

What it means

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.

Solutions

  1. Sanitize the name to contain only valid identifier characters before registering
  2. Replace invalid characters programmatically (e.g. strip non-alphanumeric characters)
  3. Generate a synthetic valid name instead of using display text

Example fix

// before
nameScopeDictionary.RegisterName("My Control 1", element);
// after
string validName = Regex.Replace("My Control 1", @"[^A-Za-z0-9_]", "");
nameScopeDictionary.RegisterName(validName, element);
Defensive patterns

Strategy: validation

Validate before calling

static readonly Regex ValidName = new(@"^[A-Za-z_][A-Za-z0-9_]*$");
if (!ValidName.IsMatch(name)) name = Sanitize(name);
nameScopeDictionary.RegisterName(name, element);

Type guard

static bool IsIdentifierName(string name) => !string.IsNullOrEmpty(name) && NameValidationHelper.IsValidIdentifierName(name);

Try / catch

try { nameScopeDictionary.RegisterName(name, element); }
catch (ArgumentException ex) { /* name not a valid identifier: ex.Message includes name */ }

Prevention

When it happens

Trigger: Calling RegisterName with names containing invalid identifier characters — spaces, dots, hyphens, leading digits, symbols.

Common situations: 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.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/0f39d910cb5f8e08. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs:47

        public NameScopeDictionary(INameScope underlyingNameScope)
        {
            _names = new FrugalObjectList<string>();
            _underlyingNameScope = underlyingNameScope ?? throw new ArgumentNullException(nameof(underlyingNameScope));
        }

        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 (_underlyingNameScope is not null)
            {
                _names.Add(name);
                _underlyingNameScope.RegisterName(name, scopedElement);
            }
            else
            {
                if (_nameMap is null)
                {
                    _nameMap = new HybridDictionary();
                    _nameMap[name] = scopedElement;
                }
                else
                {
                    object nameContext = _nameMap[name];

View on GitHub (pinned to 81131a70a4)