{"record":{"id":"15c29f66a1af4ec6","repo":"dotnet/wpf","slug":"sr-documentviewerstylemustincludecontenthost","errorCode":null,"errorMessage":"SR.DocumentViewerStyleMustIncludeContentHost","messagePattern":"SR\\.DocumentViewerStyleMustIncludeContentHost","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DocumentViewer.cs","lineNumber":2059,"sourceCode":"        /// FindContentHost does 2 things:\n        ///  - It finds \"marked\" elements (elements with ContentHost attached properties)\n        ///    in the current Visual Tree.\n        ///  - It takes these elements and populates them with the proper UI (for Content)\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">There must be a ScrollViewer in\n        /// DocumentViewer's visual tree which has the Name PART_ContentHost.</exception>\n        private void FindContentHost()\n        {\n            // Find the \"special\" element in the tree marked as the\n            //   ContentHost.  This element must exist or we throw.\n            ScrollViewer contentHost = this.Template.FindName(_contentHostName, this) as ScrollViewer;\n\n            // Make sure contentHost exists.  This wouldn't be much of a DocumentViewer if it didn't,\n            //   since we need someplace to throw our IDocumentScrollInfo so we can display documents.\n            // Throw an exception if it doesn't exist.\n            if (contentHost == null)\n            {\n                throw new NotSupportedException(SR.DocumentViewerStyleMustIncludeContentHost);\n            }\n\n            _scrollViewer = contentHost;\n            _scrollViewer.Focusable = false;\n\n            Invariant.Assert(_documentScrollInfo != null, \"IDocumentScrollInfo cannot be null.\");\n            //Make the IDSI the child of the ScrollViewer.\n            _scrollViewer.Content = _documentScrollInfo;\n            _scrollViewer.ScrollInfo = _documentScrollInfo;\n\n            // Set IDocumentScrollInfo's content if its content is invalid.\n            if (_documentScrollInfo.Content != Document)\n            {\n                AttachContent();\n            }\n        }\n\n        #region Find","sourceCodeStart":2041,"sourceCodeEnd":2077,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DocumentViewer.cs#L2041-L2077","documentation":"During template application DocumentViewer locates the part named in its template that implements the content host (an IDocumentScrollInfo host, a ScrollViewer-based Decorator). If the applied template lacks this ContentHost part, BuildWindowCore/template wiring throws NotSupportedException with SR.DocumentViewerStyleMustIncludeContentHost, because the viewer has nowhere to display documents.","triggerScenarios":"Applying a custom ControlTemplate/Style to DocumentViewer that omits the required ContentHost-named part; renaming or removing the ContentHost Decorator in a copied default template.","commonSituations":"Hand-rolled restyling of DocumentViewer from scratch; theme/template overrides where the default template was copied incompletely.","solutions":["Include the required ContentHost element (a Decorator named per DocumentViewer's template contract) in the custom ControlTemplate","Start from the default DocumentViewer template and modify it instead of building one from scratch","Keep the x:Name/ContentHost naming intact when editing the template"],"exampleFix":"// before\n<ControlTemplate TargetType=\"DocumentViewer\">\n  <Border/>\n</ControlTemplate>\n// after\n<ControlTemplate TargetType=\"DocumentViewer\">\n  <Decorator Name=\"PART_ContentHost\"/>\n</ControlTemplate>","handlingStrategy":"validation","validationCode":"// verify template defines the part: template.FindName or ensure x:Name=\"PART_ContentHost\" Decorator exists in the ControlTemplate","typeGuard":null,"tryCatchPattern":"try { viewer.ApplyTemplate(); }\ncatch (NotSupportedException) { /* template missing ContentHost part */ }","preventionTips":["Copy the default DocumentViewer template before restyling","Keep the ContentHost-named Decorator in every custom template","Test custom styles with ApplyTemplate in unit tests"],"tags":["wpf","documentviewer","template","not-supported"],"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-22T01:17:13.364Z"}