dotnet/wpf · error · ArgumentException

SR.NameScopeNameNotEmptyString

Error message

SR.NameScopeNameNotEmptyString

What it means

NameScopeDictionary.RegisterName rejects an empty name string with ArgumentException (SR.NameScopeNameNotEmptyString). XAML names must be non-empty identifiers, so empty strings are invalid arguments for registration.

Solutions

  1. Validate that the name is non-empty before calling RegisterName
  2. Trim and check the source of the name (attribute value, data binding) before use
  3. Generate a fallback name when the computed name is empty

Example fix

// before
nameScopeDictionary.RegisterName(name, element); // name may be ""
// after
if (!string.IsNullOrEmpty(name)) nameScopeDictionary.RegisterName(name, element);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(name)) throw new ArgumentException("Name must be non-empty");
nameScopeDictionary.RegisterName(name, element);

Type guard

static bool IsValidName(string name) => !string.IsNullOrEmpty(name);

Try / catch

try { nameScopeDictionary.RegisterName(name, element); }
catch (ArgumentException) { /* empty name */ }

Prevention

When it happens

Trigger: Calling RegisterName("", element) — or FindName/UnregisterName variants which apply the same check — with a zero-length name.

Common situations: x:Name attributes bound to data that evaluated to empty string; programmatically generated names where a format produced ""; parsing user input without trimming/validation.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

        public NameScopeDictionary()
        {
        }

        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;
                }

View on GitHub (pinned to 81131a70a4)