{"record":{"id":"de4bb80e6e7ef3bf","repo":"dotnet/wpf","slug":"decorator-marked-as-part-contenthost-must-have-no-content","errorCode":null,"errorMessage":"Decorator marked as PART_ContentHost must have no content.","messagePattern":"Decorator marked as PART_ContentHost must have no content\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PasswordBox.cs","lineNumber":951,"sourceCode":"\n            // Add renderScope as a child of ContentHostTemplateName\n            _renderScope = renderScope;\n            if (_passwordBoxContentHost is ScrollViewer scrollViewer)\n            {\n                if (scrollViewer.Content != null)\n                {\n                    throw new NotSupportedException(SR.TextBoxScrollViewerMarkedAsTextBoxContentMustHaveNoContent);\n                }\n                else\n                {\n                    scrollViewer.Content = _renderScope;\n                }\n            }\n            else if (_passwordBoxContentHost is Decorator decorator)\n            {\n                if (decorator.Child != null)\n                {\n                    throw new NotSupportedException(SR.TextBoxDecoratorMarkedAsTextBoxContentMustHaveNoContent);\n                }\n                else\n                {\n                    decorator.Child = _renderScope; // this may replace old render scope in case of upgrade scenario in TextBox\n                }\n            }\n            else\n            {\n                // When we implement TextContainer setting via TextView interface\n                // all text containing element will become allowed here.\n                _renderScope = null;\n\n                // Explicitly not throwing an exception here when content host = null\n                // -- designers need us to support no content scenarios\n                if (_passwordBoxContentHost != null)\n                {\n                    _passwordBoxContentHost = null;\n                    //  Remove the exception","sourceCodeStart":933,"sourceCodeEnd":969,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PasswordBox.cs#L933-L969","documentation":"The same template-application logic supports a Decorator (e.g. Border) as PART_ContentHost. If the Decorator's Child is already set, WPF throws NotSupportedException — the decorator must be empty because the internal render scope will be installed as its Child.","triggerScenarios":"Custom PasswordBox template where PART_ContentHost is a Border/Decorator with a child element inside it, evaluated when the template is applied and InitializeRenderScope runs.","commonSituations":"Decorating the content host with padding/background by nesting content inside the Border; adapting TextBox or ComboBox templates that keep children inside the content-host decorator.","solutions":["Leave the PART_ContentHost Decorator empty (no Child in XAML)","Move any visual adjustments to the Decorator's own properties (Padding, Background, CornerRadius)","Wrap with an outer element for extra visuals, keeping the named decorator childless","Start from the default PasswordBox template and modify only outer chrome"],"exampleFix":"// before\n<Border x:Name=\"PART_ContentHost\">\n  <TextBlock Text=\"inner\"/> <!-- throws -->\n</Border>\n// after\n<Border x:Name=\"PART_ContentHost\" Padding=\"4\" Background=\"White\"/>","handlingStrategy":"validation","validationCode":"var dec = templateChild as Decorator;\nbool ok = dec != null && dec.Child == null; // PART_ContentHost decorator must be empty","typeGuard":"bool isEmptyDecoratorHost(FrameworkElement fe) => fe is Decorator d && d.Child == null;","tryCatchPattern":null,"preventionTips":["Never nest children inside the PART_ContentHost Decorator","Use Decorator properties (Padding, Background) for styling instead of nested content","Keep the content-host element as the single child of its container","Diff custom templates against the default one after edits"],"tags":["wpf","passwordbox","template"],"backgroundTag":"invalid-config-value","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"}