dotnet/wpf · error · ArgumentException

SR.Format(SR.SetterValueOfMarkupExtensionNotSupported…

Error message

SR.Format(SR.SetterValueOfMarkupExtensionNotSupported, value.GetType().Name)

What it means

Styles only support DynamicResourceExtension and BindingBase-derived markup extensions as Setter values. During Seal(), WPF throws this ArgumentException if the setter's value is any other MarkupExtension (e.g. StaticResource is resolved earlier, but extensions like x:Static wrappers, TemplateBinding via code, or custom markup extensions are rejected).

Solutions

  1. Replace the custom extension value with its computed value, or use DynamicResourceExtension/Binding instead.
  2. Resolve the extension yourself (e.g. ProvideValue or fetch the resource) before assigning to the Setter's Value.
  3. If deferred/binding semantics are needed, use `new Binding { Source = ... }` or `new DynamicResourceExtension(resourceKey)`.

Example fix

// before
setter.Value = new MyCustomExtension();
// after
setter.Value = new DynamicResourceExtension("MyResourceKey");
Defensive patterns

Strategy: type-guard

Validate before calling

bool IsSupportedSetterValue(object v) => v is null || v is DeferredReference || v is DynamicResourceExtension || v is System.Windows.Data.BindingBase || v is not MarkupExtension;

Type guard

bool IsAllowedExtension(object v) => v is not MarkupExtension || v is DynamicResourceExtension || v is System.Windows.Data.BindingBase;

Try / catch

try { style.Seal(); }
catch (ArgumentException ex) when (ex.Message.Contains("markup extension")) { /* replace value */ }

Prevention

When it happens

Trigger: Setting a Setter's value in code to a custom MarkupExtension instance or an unsupported extension (anything not DynamicResourceExtension or BindingBase), then applying the style.

Common situations: Programmatic style construction passing a custom MarkupExtension; porting XAML patterns to C# where the XAML compiler would have resolved the extension; using extensions like ThemeResource-like custom providers in code.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs:112

            {
                // Setter on container is not allowed to affect the StyleProperty.
                if (dp == FrameworkElement.StyleProperty)
                {
                    throw new ArgumentException(SR.StylePropertyInStyleNotAllowed);
                }
            }

            // Value needs to be valid for the DP, or a deferred reference, or one of the supported
            // markup extensions.

            if (!dp.IsValidValue(value))
            {
                // The only markup extensions supported by styles is resources and bindings.
                if (value is MarkupExtension)
                {
                    if ( !(value is DynamicResourceExtension) && !(value is System.Windows.Data.BindingBase) )
                    {
                        throw new ArgumentException(SR.Format(SR.SetterValueOfMarkupExtensionNotSupported,
                                                           value.GetType().Name));
                    }
                }

                else if (!(value is DeferredReference))
                {
                    throw new ArgumentException(SR.Format(SR.InvalidSetterValue, value, dp.OwnerType, dp.Name));
                }
            }

            // Freeze the value for the setter
            StyleHelper.SealIfSealable(_value);

            base.Seal();
        }

        /// <summary>
        ///    Property that is being set by this setter

View on GitHub (pinned to 81131a70a4)