{"record":{"id":"3a02fa5815fe77c1","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeframeworkderived-value-name","errorCode":null,"errorMessage":"SR.Format(SR.MustBeFrameworkDerived, value.Name)","messagePattern":"SR\\.Format\\(SR\\.MustBeFrameworkDerived, value\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs","lineNumber":167,"sourceCode":"            }\n\n            set\n            {\n                // Verify Context Access\n                VerifyAccess();\n\n                if (_sealed)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"Style\"));\n                }\n\n                ArgumentNullException.ThrowIfNull(value);\n\n                if (!typeof(FrameworkElement).IsAssignableFrom(value) &&\n                    !typeof(FrameworkContentElement).IsAssignableFrom(value) &&\n                    !(DefaultTargetType == value))\n                {\n                    throw new ArgumentException(SR.Format(SR.MustBeFrameworkDerived, value.Name));\n                }\n\n                _targetType = value;\n\n                SetModified(TargetTypeID);\n            }\n        }\n\n        /// <summary>\n        ///     Style to base on\n        /// </summary>\n        [DefaultValue(null)]\n        [Ambient]\n        public Style BasedOn\n        {\n            get\n            {\n                // Verify Context Access","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs#L149-L185","documentation":"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.","triggerScenarios":"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.)","commonSituations":"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.","solutions":["Assign a Type derived from FrameworkElement (or FrameworkContentElement), e.g. typeof(Control) or typeof(TextBlock).","If the target truly is a non-element, style the container/property instead (e.g. use DataTemplate, not Style.TargetType).","For content-generated elements, target the actual generated element type (e.g. ContentPresenter's ContentTemplate)."],"exampleFix":"// before\nstyle.TargetType = typeof(MyViewModel); // not FrameworkElement-derived\n// after\nstyle.TargetType = typeof(TextBlock);","handlingStrategy":"validation","validationCode":"static bool IsValidTargetType(Type t) =>\n    typeof(FrameworkElement).IsAssignableFrom(t) ||\n    typeof(FrameworkContentElement).IsAssignableFrom(t);","typeGuard":"static bool IsFrameworkElementType(Type t) => typeof(FrameworkElement).IsAssignableFrom(t) || typeof(FrameworkContentElement).IsAssignableFrom(t);","tryCatchPattern":"try { style.TargetType = t; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"framework\")) { /* pick a FrameworkElement-derived type or use a DataTemplate */ }","preventionTips":["Style.TargetType only accepts FrameworkElement/FrameworkContentElement-derived types.","Use DataTemplate for non-element (view-model) content instead of Style.TargetType.","When generating styles programmatically, validate the Type with IsAssignableFrom first."],"tags":["invalid-argument-value","style","wpf","type-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}