dotnet/wpf · error · ArgumentException

SR.Format(SR.MarkupExtensionBadStatic, typeNameForError is…

Error message

SR.Format(SR.MarkupExtensionBadStatic, typeNameForError is not null ? $"{typeNameForError}.{_member}" : _member)

What it means

After enum handling, StaticExtension uses reflection (GetFieldOrPropertyValue) to find a public static field or property named fieldString on the resolved type. If no such public static member exists (or its value is null), it throws MarkupExtensionBadStatic, reporting typeName.member (or the raw _member).

Solutions

  1. Verify the target type has a public static field or property with exactly that name (case-sensitive).
  2. Use the enum name syntax for enum values ({x:Static ns:MyEnum.Value}) or ensure the type resolves to the enum.
  3. Check the API version still exposes the member; add it back or point to its replacement.
  4. Ensure the member is declared public; internal/protected statics are not visible.

Example fix

// before
Text="{x:Static local:Constants.MaxRetries}" // Constants has no public static MaxRetries
// after
Text="{x:Static local:Constants.DefaultMaxRetries}"
Defensive patterns

Strategy: try-catch

Validate before calling

var flags = BindingFlags.Public | BindingFlags.Static;
bool exists = type.GetField(memberName, flags) != null || type.GetProperty(memberName, flags) != null;

Type guard

static bool HasPublicStatic(Type t, string name) =>
    t.GetField(name, BindingFlags.Public | BindingFlags.Static) != null ||
    t.GetProperty(name, BindingFlags.Public | BindingFlags.Static) != null;

Try / catch

try { value = ext.ProvideValue(serviceProvider); } catch (ArgumentException ex) { /* member missing/renamed — fall back or fail the XAML load with a clear message */ }

Prevention

When it happens

Trigger: ProvideValue where the type resolves but the member is not a public static field/property: wrong casing, instance member, private member, member that was renamed or removed, or a static member whose value is null.

Common situations: Renaming a constant without updating XAML {x:Static} references; referencing a protected/internal constant; version/library upgrades removing the member; typos in member names; targeting an enum value spelled incorrectly on a non-enum type.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/StaticExtension.cs:110

                if (string.IsNullOrEmpty(typeString))
                {
                    throw new ArgumentException(SR.Format(SR.MarkupExtensionBadStatic, _member));
                }
            }

            // Use the built-in parser for enum types.
            if (type.IsEnum)
            {
                return Enum.Parse(type, fieldString);
            }

            // For other types, reflect.
            if (GetFieldOrPropertyValue(type, fieldString, out object value))
            {
                return value;
            }

            throw new ArgumentException(SR.Format(SR.MarkupExtensionBadStatic, typeNameForError is not null ? $"{typeNameForError}.{_member}" : _member));
        }

        /// <summary>
        /// Return false if a public static field or property with the same
        /// name cannot be found.
        /// <summary>
        private bool GetFieldOrPropertyValue(Type type, string name, out object value)
        {
            Type currentType = type;
            do
            {
                FieldInfo field = currentType.GetField(name, BindingFlags.Public | BindingFlags.Static);
                if (field is not null)
                {
                    value = field.GetValue(null);
                    return true;
                }

View on GitHub (pinned to 81131a70a4)