{"record":{"id":"91987dafb452feb7","repo":"dotnet/wpf","slug":"only-decorator-and-scrollviewer-can-be-used-as-part","errorCode":null,"errorMessage":"Only Decorator and ScrollViewer can be used as PART_ContentHost.","messagePattern":"Only Decorator and ScrollViewer can be used as PART_ContentHost\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PasswordBox.cs","lineNumber":970,"sourceCode":"                }\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\n                    throw new NotSupportedException(SR.PasswordBoxInvalidTextContainer);\n                }\n            }\n\n            // Attach render scope to TextEditor\n            InitializeRenderScope();\n\n            FrameworkElement element = _renderScope;\n            while (element != this && element != null)  // checking both just to be safe\n            {\n                if (element is Border)\n                {\n                    _border = (Border)element;\n                }\n                element = element.Parent as FrameworkElement;\n            }\n        }\n\n        private void ClearContentHost()","sourceCodeStart":952,"sourceCodeEnd":988,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PasswordBox.cs#L952-L988","documentation":"During template application, if PasswordBox's PART_ContentHost template part is neither a ScrollViewer nor a Decorator, WPF nulls it out and throws NotSupportedException. Only those two element types can host the password render scope; anything else (Grid, ContentControl, etc.) is rejected.","triggerScenarios":"A custom PasswordBox ControlTemplate that names some element PART_ContentHost where that element is not a ScrollViewer or Decorator (e.g. a Grid, ContentPresenter, or Border-derived non-decorator type mismatch), when the template is applied.","commonSituations":"Authoring a PasswordBox template from scratch and guessing the content-host type; renaming a Grid to PART_ContentHost; porting templates between controls with different content-host requirements.","solutions":["Change the PART_ContentHost element to a ScrollViewer or Border (Decorator)","Remove the PART_ContentHost name from invalid elements if no content host is needed","Copy the default PasswordBox ControlTemplate as the starting point so the part types are correct","Verify the element declared in the template: ScrollViewer for scrolling text, Border/Decorator for simple hosting"],"exampleFix":"// before\n<Grid x:Name=\"PART_ContentHost\"/> <!-- throws -->\n// after\n<ScrollViewer x:Name=\"PART_ContentHost\"/>","handlingStrategy":"validation","validationCode":"var host = template.FindName(\"PART_ContentHost\", passwordBox) as FrameworkElement;\nbool ok = host is ScrollViewer || host is Decorator; // must be one of these two types","typeGuard":"bool isValidContentHost(FrameworkElement fe) => fe is ScrollViewer || fe is Decorator;","tryCatchPattern":null,"preventionTips":["Name only ScrollViewer or Border/Decorator elements as PART_ContentHost","Never rename Grid/ContentPresenter to PART_ContentHost in a PasswordBox template","Derive custom templates from the default PasswordBox template","Verify template part types whenever restyling edit-capable controls"],"tags":["wpf","passwordbox","template"],"backgroundTag":"type-mismatch","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"}