dotnet/wpf · error · InvalidOperationException

SR.MustHaveName

Error message

SR.MustHaveName

What it means

NameReferenceConverter.ConvertFrom throws InvalidOperationException (SR.MustHaveName) when the value being converted is null, not a string, or an empty string. A non-empty name is required to look up the referenced object via IXamlNameResolver. The library validates the value before attempting resolution.

Solutions

  1. Pass a non-empty string as the conversion value.
  2. Validate/trim the source string before invoking the converter and short-circuit empty values.
  3. If the name is optional in your scenario, guard the conversion call and skip it when the value is empty.

Example fix

// before
converter.ConvertFrom(context, culture, ""); // throws
// after
if (!string.IsNullOrEmpty(name))
{
    var obj = converter.ConvertFrom(context, culture, name);
}
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(value as string))
    return null; // or throw a domain-specific error before calling the converter

Type guard

bool IsConvertibleName(object v) => v is string s && s.Length > 0;

Try / catch

try { var obj = converter.ConvertFrom(context, culture, name); }
catch (InvalidOperationException) { /* name was null/empty — handle missing reference */ }

Prevention

When it happens

Trigger: Calling ConvertFrom with value = null, value = "", or a non-string object (e.g. an int) that cannot be treated as a name string.

Common situations: XAML content where a name reference attribute is present but empty (Name=""); binding generators passing non-string tokens; data-driven XAML where the name source field was blank.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/NameReferenceConverter.cs:37

            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            ArgumentNullException.ThrowIfNull(context);

            var nameResolver = (IXamlNameResolver)context.GetService(typeof(IXamlNameResolver));
            if (nameResolver is null)
            {
                throw new InvalidOperationException(SR.MissingNameResolver);
            }

            string name = value as string;
            if (string.IsNullOrEmpty(name))
            {
                throw new InvalidOperationException(SR.MustHaveName);
            }

            object obj = nameResolver.Resolve(name);
            if (obj is null)
            {
                string[] names = new string[] { name };
                obj = nameResolver.GetFixupToken(names, true);
            }

            return obj;
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (context is null || (context.GetService(typeof(IXamlNameProvider)) as  IXamlNameProvider) is null)
            {
                return false;
            }

View on GitHub (pinned to 81131a70a4)