dotnet/wpf · error · InvalidOperationException

SR.BamlBadExtensionValue

Error message

SR.BamlBadExtensionValue

What it means

BamlReader throws InvalidOperationException(SR.BamlBadExtensionValue) while resolving a markup-extension property from BAML when the referenced known property ID does not map to a DependencyProperty (dp stays null after KnownTypes lookup). It means the compiled BAML references a property the reader cannot resolve to a known DependencyProperty, so the reader cannot compute owner/property names.

Solutions

  1. Recompile the XAML/BAML with the same .NET/WPF version used at runtime so KnownTypes IDs match
  2. Verify the targeted property is a DependencyProperty (replaces other property kinds after refactors)
  3. Regenerate/rebuild the assembly containing the BAML instead of editing it
  4. If loading loose XAML, use XamlReader with a matching SchemaContext instead of BamlReader

Example fix

// before: markup extension set on a plain CLR property
<Label MyThing="{StaticResource x}" />
// after: target a DependencyProperty
<Label Content="{StaticResource x}" />
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure markup extension targets a DependencyProperty before compiling/loading
var prop = typeof(MyControl).GetProperty(nameof(MyControl.MyProp));
if (DependencyProperty.FromName(nameof(MyControl.MyProp), typeof(MyControl)) == null)
    throw new InvalidOperationException("Target must be a DependencyProperty");

Type guard

static bool IsDependencyProperty(Type t, string name) =>
    DependencyProperty.FromName(name, t) != null;

Try / catch

try { LoadBaml(stream); }
catch (InvalidOperationException ex) when (ex.Message.Contains("extension")) {
    // recompile BAML with matching framework, or fix the extension target property
}

Prevention

When it happens

Trigger: Reading compiled BAML whose PropertyRecord resolves via a KnownTypes ID to a non-dependency-property (or an unknown ID), leaving dp == null in ReadPropertyExtensionRecord/related extension-value handling in BamlReader.cs.

Common situations: BAML compiled by a mismatched or older/newer WPF toolchain (known-type table out of sync), hand-modified or corrupted BAML, or markup extensions targeting properties that are not DependencyProperties after refactoring.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlReader.cs:2391

            string valuePrefix = null;
            string typeName = null;
            string propName = null;

            if (memberId < 0)
            {
                memberId = (short)-memberId;
                DependencyProperty dp = null;
                if (memberId < (short)KnownProperties.MaxDependencyProperty)
                {
                    KnownProperties knownId = (KnownProperties)(memberId);
                    {
                        dp = KnownTypes.GetKnownDependencyPropertyFromId(knownId);
                    }
                }

                if (dp == null)
                {
                    throw new InvalidOperationException(SR.BamlBadExtensionValue);
                }
                else
                {
                    typeName = dp.OwnerType.Name;
                    propName = dp.Name;
                }

                object prefixObject = _prefixDictionary[XamlReaderHelper.DefaultNamespaceURI];
                valuePrefix = (prefixObject == null) ? string.Empty : (string)prefixObject;
            }
            else
            {
                BamlAttributeInfoRecord attrInfo = MapTable.GetAttributeInfoFromId(memberId);
                BamlTypeInfoRecord valueTypeInfo = MapTable.GetTypeInfoFromId(attrInfo.OwnerTypeId);
                string valueXmlNamespace;
                string valueAssemblyName;
                GetAssemblyAndPrefixAndXmlns(valueTypeInfo, out valueAssemblyName, out valuePrefix, out valueXmlNamespace);
                typeName = valueTypeInfo.TypeFullName;

View on GitHub (pinned to 81131a70a4)