dotnet/wpf · error · ArgumentException

SR.NameScopeNameNotEmptyString

Error message

SR.NameScopeNameNotEmptyString

What it means

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.

Solutions

  1. Validate string.IsNullOrEmpty(name) before calling RegisterName and reject early with a meaningful message.
  2. Ensure the data source supplying the name provides a non-empty value.
  3. Generate a fallback name (e.g. "element1") when the supplied name is empty.
  4. Catch ArgumentException and report the invalid name to the caller.

Example fix

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

Strategy: validation

Validate before calling

bool valid = !string.IsNullOrEmpty(name);

Type guard

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

Try / catch

try { scope.RegisterName(name, element); }
catch (ArgumentException ex) { /* name was empty or invalid */ }

Prevention

When it happens

Trigger: Calling RegisterName("", element) on a NameScope or via INameScope; binding a Name property from data where the value resolved to empty.

Common situations: Data-bound names where the source field is empty; string manipulation that stripped the name; deserializing XAML with a missing x:Name value.

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/fbc7ec581a4e800c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/NameScope.cs:42

    /// within the logical tree section.
    /// </summary>
    [TypeForwardedFrom("PresentationFramework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
    public class NameScope : INameScopeDictionary
    {        
        #region INameScope
        
        /// <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 == null)
            {
                _nameMap = new HybridDictionary();
                _nameMap[name] = scopedElement;
            }
            else
            {
                object nameContext = _nameMap[name];
                // first time adding the Name, set it
                if (nameContext == null)
                {
                    _nameMap[name] = scopedElement;

View on GitHub (pinned to 81131a70a4)