{"record":{"id":"b2e9304985fae97c","repo":"dotnet/wpf","slug":"sr-textboxbase","errorCode":null,"errorMessage":"SR.TextBoxScrollViewerMarkedAsTextBoxContentMustHaveNoContent","messagePattern":"SR\\.TextBoxScrollViewerMarkedAsTextBoxContentMustHaveNoContent","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/TextBoxBase.cs","lineNumber":1901,"sourceCode":"            ClearContentHost();\n\n            // Find ContentHostTemplateName in the style\n            _textBoxContentHost = GetTemplateChild(ContentHostTemplateName) as FrameworkElement;\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            // TextBox goes into disabled state, but not throw.\n\n            // Add renderScope as a child of ContentHostTemplateName\n            _renderScope = renderScope;\n            if (_textBoxContentHost is ScrollViewer scrollViewer)\n            {\n                if (scrollViewer.Content != null)\n                {\n                    _renderScope = null;\n                    _textBoxContentHost = null;\n                    //  Do not throw exception\n                    throw new NotSupportedException(SR.TextBoxScrollViewerMarkedAsTextBoxContentMustHaveNoContent);\n                }\n                else\n                {\n                    scrollViewer.Content = _renderScope; // this may replace old render scope in case of upgrade scenario in TextBox\n                }\n            }\n            else if (_textBoxContentHost is Decorator decorator)\n            {\n                if (decorator.Child != null)\n                {\n                    _renderScope = null;\n                    _textBoxContentHost = null;\n                    //  Do not throw exception\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","sourceCodeStart":1883,"sourceCodeEnd":1919,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/TextBoxBase.cs#L1883-L1919","documentation":"When TextBoxBase finds a ScrollViewer marked as its text-box content part (via template parts) whose Content is already set, it aborts attaching the internal render scope and throws NotSupportedException. A ScrollViewer designated as PART content host must have no other content, since TextBoxBase installs its own editing scope into it.","triggerScenarios":"Applying a custom ControlTemplate for TextBox/TextBoxBase where the ScrollViewer part already contains child elements (x:Bind content, injected children, or leftover content from a modified template).","commonSituations":"Hand-edited or third-party TextBox templates that place extra visuals directly inside the content-host ScrollViewer instead of in an adorner/overlay layer; template upgrades between framework versions.","solutions":["Remove all content from the ScrollViewer part in the template; host extra visuals in a sibling Grid overlay instead.","Name the ScrollViewer part correctly (PART_ContentHost) and leave its Content empty so TextBoxBase can attach its render scope.","Base custom templates on the default TextBox template and only modify styling, not the part structure."],"exampleFix":"// before\n<ScrollViewer x:Name=\"PART_ContentHost\">\n    <TextBlock Text=\"extra\"/> <!-- content present -> throws -->\n</ScrollViewer>\n// after\n<Grid>\n    <ScrollViewer x:Name=\"PART_ContentHost\"/>\n    <TextBlock Text=\"extra\" IsHitTestVisible=\"False\"/>\n</Grid>","handlingStrategy":"validation","validationCode":"// In the template, before apply:\nbool contentHostIsEmpty = scrollViewerPart.Content == null;\nif (!contentHostEmpty) throw new InvalidOperationException(\"PART_ContentHost ScrollViewer must have no content\");","typeGuard":"static bool IsValidTextBoxContentHost(ScrollViewer sv) => sv != null && sv.Content == null;","tryCatchPattern":"try\n{\n    textBox.Template = customTemplate;\n    textBox.ApplyTemplate();\n}\ncatch (NotSupportedException)\n{\n    textBox.Template = null; // revert to default template\n}","preventionTips":["Never place children directly inside the PART_ContentHost ScrollViewer","Use sibling overlay elements for watermarks/badges","Base custom templates on the extracted default template"],"tags":["wpf","textbox","template","not-supported"],"backgroundTag":"invalid-argument-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"}