{"record":{"id":"b2c0c78cfebd8e30","repo":"dotnet/wpf","slug":"sr-format-sr-styletargettypemismatchwithelement-this","errorCode":null,"errorMessage":"SR.Format(SR.StyleTargetTypeMismatchWithElement, this.TargetType.Name, elementType.Name)","messagePattern":"SR\\.Format\\(SR\\.StyleTargetTypeMismatchWithElement, this\\.TargetType\\.Name, elementType\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs","lineNumber":469,"sourceCode":"                    Property = dp,\n                    ValueInternal = value\n                };\n\n                PropertyValues.Add(propertyValue);\n            }\n        }\n\n        internal void CheckTargetType(object element)\n        {\n            // In the most common case TargetType is Default\n            // and we can avoid a call to IsAssignableFrom() who's performance is unknown.\n            if(DefaultTargetType == TargetType)\n                return;\n\n            Type elementType = element.GetType();\n            if(!TargetType.IsAssignableFrom(elementType))\n            {\n                throw new InvalidOperationException(SR.Format(SR.StyleTargetTypeMismatchWithElement,\n                                                    this.TargetType.Name,\n                                                    elementType.Name));\n            }\n        }\n\n        /// <summary>\n        /// This Style and all factories/triggers are now immutable\n        /// </summary>\n        public void Seal()\n        {\n            // Verify Context Access\n            VerifyAccess();\n\n            // 99% case - Style is already sealed.\n            if (_sealed)\n            {\n                return;\n            }","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs#L451-L487","documentation":"CheckTargetType verifies that the TargetType declared on a Style is assignable from the type of the element the style is being applied to. When an element whose type is not derived from TargetType receives this style, InvalidOperationException is thrown with both type names.","triggerScenarios":"Setting element.Style (or an implicit style lookup) to a Style whose TargetType (e.g. Button) does not match or is not a base class of the element's actual type (e.g. CheckBox element receiving a Button-targeted style).","commonSituations":"Copy-pasting styles between controls without changing TargetType; sharing a ResourceDictionary across projects where control types differ; applying an implicit-style keyed resource to a differently-typed element via Style=\"{StaticResource ...}\".","solutions":["Change the style's TargetType to the element's type or one of its base types.","Apply the style only to elements assignable to TargetType.","Use a common base type (e.g. Control or FrameworkElement) as TargetType when one style should serve multiple control types, adjusting Setter properties accordingly."],"exampleFix":"// before\nvar style = new Style(typeof(Button));\ncheckBox.Style = style; // throws\n// after\nvar style = new Style(typeof(CheckBox));\ncheckBox.Style = style;","handlingStrategy":"validation","validationCode":"if (element != null && style.TargetType != null && !style.TargetType.IsAssignableFrom(element.GetType()))\n    throw new InvalidOperationException($\"Style targets {style.TargetType.Name}, element is {element.GetType().Name}\");\nelement.Style = style;","typeGuard":"static bool StyleFitsElement(Style s, FrameworkElement e) => s?.TargetType?.IsAssignableFrom(e.GetType()) ?? true;","tryCatchPattern":"try { element.Style = style; }\ncatch (InvalidOperationException ex) { log.Error(\"TargetType mismatch\", ex); }","preventionTips":["Check TargetType matches (or is a base of) the element type before assigning Style.","When sharing styles across controls, key off a common base type.","Avoid copy-pasting styles without updating TargetType."],"tags":["wpf","style","targettype","type-mismatch"],"backgroundTag":"type-mismatch","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"}