dotnet/wpf · error · ArgumentException

SR.NameNotEmptyString

Error message

SR.NameNotEmptyString

What it means

The FrameworkElementFactory.Name setter throws this ArgumentException when assigned string.Empty. An empty string is not a valid element name; the factory requires either null (no name) or a non-empty name string. This keeps template part naming (TemplatePart/name registration) well-formed.

Solutions

  1. Pass a non-empty name, or pass null when the factory should be unnamed.
  2. Validate/trim the name string before assignment and fall back to null if empty.
  3. Fix the upstream generator that produces empty names.

Example fix

// before
fef.Name = userSuppliedName; // throws when ""

// after
var name = string.IsNullOrWhiteSpace(userSuppliedName) ? null : userSuppliedName.Trim();
fef.Name = name; // null means unnamed, non-empty is accepted
Defensive patterns

Strategy: validation

Validate before calling

if (name is "") throw new ArgumentException("Name must be null or non-empty, not empty string.");
factory.Name = name;

Type guard

string? NormalizeName(string? s) => string.IsNullOrEmpty(s) ? null : s;

Try / catch

try { factory.Name = name; }
catch (ArgumentException ex) { logger.LogError(ex, "Empty name supplied for factory"); }

Prevention

When it happens

Trigger: factory.Name = string.Empty; or a computed name variable that evaluates to "" (e.g. string.Concat of missing parts, or Name = someString where someString == "").

Common situations: Names sourced from configuration or data-driven template generation where the value is present but empty; sanitization code that strips the name to nothing before assignment.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs:144

                _text = value;
            }
        }

        /// <summary>
        ///     Style identifier
        /// </summary>
        public string Name
        {
            get { return _childName; }
            set
            {
                if (_sealed)
                {
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "FrameworkElementFactory"));
                }
                if (value == string.Empty)
                {
                    throw new ArgumentException(SR.NameNotEmptyString);
                }

                _childName = value;
            }
        }


        /// <summary>
        ///     Add a factory child to this factory
        /// </summary>
        /// <param name="child">Child to add</param>
        public void AppendChild(FrameworkElementFactory child)
        {
            if (_sealed)
            {
                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "FrameworkElementFactory"));
            }

View on GitHub (pinned to 81131a70a4)