{"record":{"id":"46b6bde25112faf4","repo":"dotnet/wpf","slug":"scrollviewer-marked-as-part-contenthost-must-have-no-content","errorCode":null,"errorMessage":"ScrollViewer marked as PART_ContentHost must have no content.","messagePattern":"ScrollViewer 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":940,"sourceCode":"        {\n            // Clear the content host from previous render scope (if any)\n            ClearContentHost();\n\n            // Find ContentHostTemplateName in the style\n            _passwordBoxContentHost = GetTemplateChild(ContentHostTemplateName) as FrameworkElement;\n\n            // Note that we allow ContentHostTemplateName to be optional.\n            // This simplifies toolability of our control styling.\n            // When the ContentHostTemplateName is not found or incorrect\n            // PasswordBox goes into disabled state, but does not throw.\n\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","sourceCodeStart":922,"sourceCodeEnd":958,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PasswordBox.cs#L922-L958","documentation":"When PasswordBox applies its control template, SetRenderScopeToContentHost attaches the internal PasswordTextBox render scope to the PART_ContentHost element. If that part is a ScrollViewer that already has Content, WPF throws NotSupportedException because the content host must be empty to host the password render scope.","triggerScenarios":"A custom PasswordBox ControlTemplate whose ScrollViewer named PART_ContentHost contains child content (elements or inline content set on ScrollViewer.Content), triggering template application (InitializeRenderScope).","commonSituations":"Copying a TextBox template and adding decorations inside the PART_ContentHost ScrollViewer; blending a TextBlock watermark inside the content host; hand-edited templates after upgrading from a TextBox template.","solutions":["Remove all content from the PART_ContentHost ScrollViewer in the template","Add watermarks/hints as sibling elements overlaying the content host, not as its Content","Use TemplateBinding-free, empty ScrollViewer exactly named PART_ContentHost with ScrollViewer.Content = null","Base the custom template on the stock PasswordBox template rather than the TextBox one"],"exampleFix":"// before\n<ScrollViewer x:Name=\"PART_ContentHost\">\n  <TextBlock Text=\"Enter password\"/> <!-- throws -->\n</ScrollViewer>\n// after\n<Grid>\n  <ScrollViewer x:Name=\"PART_ContentHost\"/>\n  <TextBlock Text=\"Enter password\" IsHitTestVisible=\"False\" Visibility=\"{Binding Text, Converter=...}\"/>\n</Grid>","handlingStrategy":"validation","validationCode":"var sv = templateChild as ScrollViewer;\nbool ok = sv != null && sv.Content == null; // PART_ContentHost must be an empty ScrollViewer","typeGuard":"bool isEmptyContentHost(FrameworkElement fe) =>\n    (fe is ScrollViewer sv && sv.Content == null) || (fe is Decorator d && d.Child == null);","tryCatchPattern":null,"preventionTips":["Keep PART_ContentHost ScrollViewer childless in PasswordBox templates","Implement watermark/placeholder visuals as overlay siblings, not content-host children","Start custom templates from the stock PasswordBox template","Avoid reusing TextBox templates verbatim for PasswordBox"],"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"}