{"record":{"id":"e1c4d26c30456b84","repo":"dotnet/wpf","slug":"sr-textboxinvalidtextcontainer","errorCode":null,"errorMessage":"SR.TextBoxInvalidTextContainer","messagePattern":"SR\\.TextBoxInvalidTextContainer","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/TextBoxBase.cs","lineNumber":1934,"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 (_textBoxContentHost != null)\n                {\n                    _textBoxContentHost = null;\n                    //  Remove the exception\n                    throw new NotSupportedException(SR.TextBoxInvalidTextContainer);\n                }\n            }\n\n            // Attach render scope to TextEditor\n            InitializeRenderScope();\n        }\n\n        private void ClearContentHost()\n        {\n            // Detach render scope from TextEditor\n            UninitializeRenderScope();\n\n            // Render scope has been created by us,\n            // so we need to extract if from visual tree.\n            if (_textBoxContentHost is ScrollViewer)\n            {\n                ((ScrollViewer)_textBoxContentHost).Content = null;\n            }","sourceCodeStart":1916,"sourceCodeEnd":1952,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/TextBoxBase.cs#L1916-L1952","documentation":"When the element registered as the TextBox content host turns out not to be a usable text container (it is not a valid render scope such as ScrollViewer/Decorator/Panel as expected), TextBoxBase nulls out the host and throws NotSupportedException. Designers require a no-content scenario to be tolerated, hence the nulling before the throw.","triggerScenarios":"Applying a template whose PART_ContentHost element is of an unsupported type (e.g. a raw ContentPresenter or custom FrameworkElement instead of ScrollViewer/Decorator/Panel), so the created text container is invalid.","commonSituations":"Custom control templates that rename or retype the content host; migrating templates across WPF versions where part requirements changed.","solutions":["Make PART_ContentHost a supported type - ScrollViewer for TextBox, or a Decorator/Panel as the template expects.","Restore the default part structure from the framework's default template.","If a no-content design is intentional, ensure no element is incorrectly named as the content host so designers can render without one."],"exampleFix":"// before\n<ContentPresenter x:Name=\"PART_ContentHost\"/> <!-- unsupported type -> throws -->\n// after\n<ScrollViewer x:Name=\"PART_ContentHost\"/>","handlingStrategy":"validation","validationCode":"var host = template.FindName(\"PART_ContentHost\", textBox) as FrameworkElement;\nbool isValid = host is ScrollViewer || host is Decorator || host is Panel;","typeGuard":"static bool IsSupportedContentHost(FrameworkElement fe) => fe is ScrollViewer || fe is Decorator || fe is Panel;","tryCatchPattern":"try\n{\n    textBox.ApplyTemplate();\n}\ncatch (NotSupportedException)\n{\n    textBox.Template = (ControlTemplate)Application.Current.Resources[\"DefaultTextBoxTemplate\"];\n}","preventionTips":["Use ScrollViewer/Decorator/Panel for PART_ContentHost","Do not rename or retype framework template parts","Test custom templates in design and runtime scenarios"],"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"}