dotnet/wpf · error · ArgumentException

SR.Format(SR.ParserPrefixNSProperty, nsPrefix, name)

Error message

SR.Format(SR.ParserPrefixNSProperty, nsPrefix, name)

What it means

GetTypeFromName throws this ArgumentException when a qualified type name 'prefix:TypeName' in the property path uses an XML namespace prefix that is not present in ParserContext.XmlnsDictionary. Since no namespace URI can be found for the prefix, the type cannot be looked up via the XamlTypeMapper and path resolution fails.

Solutions

  1. Add the missing prefix to ParserContext.XmlnsDictionary, e.g. ctx.XmlnsDictionary["local"] = "clr-namespace:MyNs;assembly=MyAssembly"
  2. Use a prefix that already exists in XmlnsDictionary (inspect its keys) and rewrite the path accordingly
  3. Remove the prefix and reference a type resolvable via the default namespace if applicable
  4. When building contexts in code, replicate all xmlns declarations from the source XAML before path resolution

Example fix

// before
var ctx = new ParserContext();
var path = new PropertyPath("(local:MyClass.MyProp)", ctx); // throws
// after
var ctx = new ParserContext();
ctx.XmlnsDictionary["local"] = "clr-namespace:MyApp.Classes;assembly=MyApp";
var path = new PropertyPath("(local:MyClass.MyProp)", ctx);
Defensive patterns

Strategy: validation

Validate before calling

bool prefixRegistered = parserContext.XmlnsDictionary.Contains("local");
// register if missing:
if (!prefixRegistered) parserContext.XmlnsDictionary["local"] = "clr-namespace:MyApp;assembly=MyApp";

Type guard

bool PrefixResolvable(string qualifiedName, ParserContext ctx) =>
    !qualifiedName.Contains(':') || ctx.XmlnsDictionary.Contains(qualifiedName.Split(':')[0]);

Try / catch

try { var t = GetTypeFromName(qualifiedName, parserContext); }
catch (ArgumentException ex) when (ex.Message.Contains("namespace") || ex.Message.Contains("prefix")) { /* add xmlns mapping */ }

Prevention

When it happens

Trigger: Path '(local:MyClass.MyProp)' where XmlnsDictionary has no 'local' entry; using a prefix defined in the XAML document but not propagated into the manually constructed ParserContext; typo in the prefix before the colon.

Common situations: Programmatically creating Binding paths with a ParserContext while forgetting to copy xmlns mappings from the XAML; custom clr-namespace prefixes lost when paths are evaluated outside XAML parsing; renamed prefixes after refactoring XAML files.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/PropertyPath.cs:839

            {
                // Find the namespace prefix
                string nsPrefix;
                int nsIndex = name.IndexOf(':');
                if (nsIndex == -1)
                    nsPrefix = string.Empty;
                else
                {
                    // Found a namespace prefix separator, so create replacement _pathString.
                    // String processing - split "foons" from "BarClass.BazProp"
                    nsPrefix = name.Substring(0, nsIndex).TrimEnd();
                    name = name.Substring(nsIndex + 1).TrimStart();
                }

                // Find the namespace URI, even if its the default one
                string namespaceURI = parserContext.XmlnsDictionary[nsPrefix];
                if (namespaceURI == null)
                {
                    throw new ArgumentException(SR.Format(SR.ParserPrefixNSProperty, nsPrefix, name));
                }

                TypeAndSerializer typeAndSerializer = parserContext.XamlTypeMapper.GetTypeOnly(namespaceURI, name);

                return typeAndSerializer?.ObjectType;
            }

            else
            {
                if (context is IServiceProvider)
                {

                    IXamlTypeResolver xtr = (context as IServiceProvider).GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;

                    if (xtr != null)
                    {
                        return xtr.Resolve(name);
                    }

View on GitHub (pinned to 81131a70a4)