{"record":{"id":"7c1bbacf14152f21","repo":"dotnet/wpf","slug":"sr-format-sr-nullpropertyillegal-targettype","errorCode":null,"errorMessage":"SR.Format(SR.NullPropertyIllegal, \"TargetType\")","messagePattern":"SR\\.Format\\(SR\\.NullPropertyIllegal, \"TargetType\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs","lineNumber":493,"sourceCode":"        /// <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            }\n\n            // Most parameter checking is done as \"upstream\" as possible, but some\n            //  can't be checked until Style is sealed.\n            if (_targetType == null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.NullPropertyIllegal, \"TargetType\"));\n            }\n\n            if (_basedOn != null)\n            {\n                if(DefaultTargetType != _basedOn.TargetType &&\n                    !_basedOn.TargetType.IsAssignableFrom(_targetType))\n                {\n                    throw new InvalidOperationException(SR.Format(SR.MustBaseOnStyleOfABaseType, _targetType.Name));\n                }\n            }\n\n            // Seal setters\n            _setters?.Seal();\n\n            // Seal triggers\n            _visualTriggers?.Seal();\n\n            // Will throw InvalidOperationException if we find a loop of","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs#L475-L511","documentation":"Seal() validates the Style before freezing it for use. If TargetType was never set (null), Seal throws InvalidOperationException because a sealed style must know which element type it targets to support property validation and lookup.","triggerScenarios":"Calling style.Seal() explicitly, or applying a style whose TargetType was never assigned (e.g. new Style() with no argument, or XAML <Style> without TargetType being put into use).","commonSituations":"Creating styles programmatically and forgetting TargetType; XAML styles relying on TargetType inference that fail when used from code; dynamically built styles in helpers/tests that are applied without setting TargetType.","solutions":["Set TargetType in the Style constructor: new Style(typeof(MyControl)).","Add TargetType=\"{x:Type ns:MyControl}\" to the XAML Style declaration.","Validate the style (TargetType != null) before applying it or calling Seal()."],"exampleFix":"// before\nvar style = new Style();\nstyle.Setters.Add(new Setter(Control.ForegroundProperty, Brushes.Red));\nstyle.Seal(); // throws\n// after\nvar style = new Style(typeof(Button));\nstyle.Setters.Add(new Setter(Control.ForegroundProperty, Brushes.Red));\nstyle.Seal();","handlingStrategy":"validation","validationCode":"if (style.TargetType == null)\n    throw new InvalidOperationException(\"Set TargetType before sealing/applying the style.\");\nstyle.Seal();","typeGuard":"static bool IsReadyToSeal(Style s) => s != null && s.TargetType != null;","tryCatchPattern":"try { style.Seal(); }\ncatch (InvalidOperationException ex) { log.Error(\"Style missing TargetType\", ex); }","preventionTips":["Always pass TargetType in the Style constructor.","In XAML, always set TargetType=\"{x:Type ...}\" on Style.","Validate programmatically built styles before applying them."],"tags":["wpf","style","null","sealing"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}