dotnet/wpf · error · ArgumentException

SR.Format(SR.MustBeFrameworkDerived, value.Name)

Error message

SR.Format(SR.MustBeFrameworkDerived, value.Name)

What it means

The Style.TargetType setter throws ArgumentException (SR.MustBeFrameworkDerived with the type's Name) when the assigned Type is not derived from FrameworkElement, FrameworkContentElement, or equal to the DefaultTargetType. TargetType must reference a valid WPF element type.

Solutions

  1. Assign a Type derived from FrameworkElement (or FrameworkContentElement), e.g. typeof(Control) or typeof(TextBlock).
  2. If the target truly is a non-element, style the container/property instead (e.g. use DataTemplate, not Style.TargetType).
  3. For content-generated elements, target the actual generated element type (e.g. ContentPresenter's ContentTemplate).

Example fix

// before
style.TargetType = typeof(MyViewModel); // not FrameworkElement-derived
// after
style.TargetType = typeof(TextBlock);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidTargetType(Type t) =>
    typeof(FrameworkElement).IsAssignableFrom(t) ||
    typeof(FrameworkContentElement).IsAssignableFrom(t);

Type guard

static bool IsFrameworkElementType(Type t) => typeof(FrameworkElement).IsAssignableFrom(t) || typeof(FrameworkContentElement).IsAssignableFrom(t);

Try / catch

try { style.TargetType = t; }
catch (ArgumentException ex) when (ex.Message.Contains("framework")) { /* pick a FrameworkElement-derived type or use a DataTemplate */ }

Prevention

When it happens

Trigger: Setting style.TargetType = typeof(SomeNonElementClass) — e.g. a plain CLR class, an interface, or a non-WPF type — in code. (XAML blocks this earlier via type resolution.)

Common situations: Creating styles programmatically for view-model or helper classes; typos or refactors that swap in the wrong Type; targeting ContentPresenter content types that are not framework elements.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs:167

            }

            set
            {
                // Verify Context Access
                VerifyAccess();

                if (_sealed)
                {
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "Style"));
                }

                ArgumentNullException.ThrowIfNull(value);

                if (!typeof(FrameworkElement).IsAssignableFrom(value) &&
                    !typeof(FrameworkContentElement).IsAssignableFrom(value) &&
                    !(DefaultTargetType == value))
                {
                    throw new ArgumentException(SR.Format(SR.MustBeFrameworkDerived, value.Name));
                }

                _targetType = value;

                SetModified(TargetTypeID);
            }
        }

        /// <summary>
        ///     Style to base on
        /// </summary>
        [DefaultValue(null)]
        [Ambient]
        public Style BasedOn
        {
            get
            {
                // Verify Context Access

View on GitHub (pinned to 81131a70a4)