{"record":{"id":"8b8728fab8fdb075","repo":"dotnet/wpf","slug":"sr-musthavename","errorCode":null,"errorMessage":"SR.MustHaveName","messagePattern":"SR\\.MustHaveName","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/NameReferenceConverter.cs","lineNumber":37,"sourceCode":"            }\n\n            return base.CanConvertFrom(context, sourceType);\n        }\n\n        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)\n        {\n            ArgumentNullException.ThrowIfNull(context);\n\n            var nameResolver = (IXamlNameResolver)context.GetService(typeof(IXamlNameResolver));\n            if (nameResolver is null)\n            {\n                throw new InvalidOperationException(SR.MissingNameResolver);\n            }\n\n            string name = value as string;\n            if (string.IsNullOrEmpty(name))\n            {\n                throw new InvalidOperationException(SR.MustHaveName);\n            }\n\n            object obj = nameResolver.Resolve(name);\n            if (obj is null)\n            {\n                string[] names = new string[] { name };\n                obj = nameResolver.GetFixupToken(names, true);\n            }\n\n            return obj;\n        }\n\n        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\n            if (context is null || (context.GetService(typeof(IXamlNameProvider)) as  IXamlNameProvider) is null)\n            {\n                return false;\n            }","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/NameReferenceConverter.cs#L19-L55","documentation":"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.","triggerScenarios":"Calling ConvertFrom with value = null, value = \"\", or a non-string object (e.g. an int) that cannot be treated as a name string.","commonSituations":"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.","solutions":["Pass a non-empty string as the conversion value.","Validate/trim the source string before invoking the converter and short-circuit empty values.","If the name is optional in your scenario, guard the conversion call and skip it when the value is empty."],"exampleFix":"// before\nconverter.ConvertFrom(context, culture, \"\"); // throws\n// after\nif (!string.IsNullOrEmpty(name))\n{\n    var obj = converter.ConvertFrom(context, culture, name);\n}","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(value as string))\n    return null; // or throw a domain-specific error before calling the converter","typeGuard":"bool IsConvertibleName(object v) => v is string s && s.Length > 0;","tryCatchPattern":"try { var obj = converter.ConvertFrom(context, culture, name); }\ncatch (InvalidOperationException) { /* name was null/empty — handle missing reference */ }","preventionTips":["Trim and validate name strings at the data source before conversion.","Treat empty name-reference attributes in XAML as authoring errors caught by schema validation.","Skip conversion calls entirely when the value is null/empty instead of letting the converter throw."],"tags":["empty-value","xaml","name-resolution","invalid-value"],"backgroundTag":"empty-required-field","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}