{"record":{"id":"027d0daa7cd04305","repo":"dotnet/wpf","slug":"sr-adornedelementplaceholdermustbeintemplate","errorCode":null,"errorMessage":"SR.AdornedElementPlaceholderMustBeInTemplate","messagePattern":"SR\\.AdornedElementPlaceholderMustBeInTemplate","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs","lineNumber":165,"sourceCode":"        /// <summary>\n        /// Returns enumerator to logical children.\n        /// </summary>\n        protected internal override IEnumerator LogicalChildren\n        {\n            get\n            {\n                // Could optimize this code by returning EmptyEnumerator.Instance if _child == null.\n                return new SingleChildEnumerator(_child);\n            }\n        }\n\n        /// <summary>\n        ///     This virtual method in called when IsInitialized is set to true and it raises an Initialized event\n        /// </summary>\n        protected override void OnInitialized(EventArgs e)\n        {\n            if (TemplatedParent == null)\n                throw new InvalidOperationException(SR.AdornedElementPlaceholderMustBeInTemplate);\n\n            base.OnInitialized(e);\n        }\n\n\n        /// <summary>\n        ///     AdornedElementPlaceholder measure behavior is to measure\n        ///     only the first visual child.  Note that the return value\n        ///     of Measure on this child is ignored as the purpose of this\n        ///     class is to match the size of the element for which this\n        ///     is a placeholder.\n        /// </summary>\n        /// <param name=\"constraint\">The measurement constraints.</param>\n        /// <returns>The desired size of the control.</returns>\n        protected override Size MeasureOverride(Size constraint)\n        {\n            if (TemplatedParent == null)\n                throw new InvalidOperationException(SR.AdornedElementPlaceholderMustBeInTemplate);","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs#L147-L183","documentation":"AdornedElementPlaceholder is a control that only makes sense as part of a Validation error template; OnInitialized throws InvalidOperationException when the control is created with no TemplatedParent (i.e. instantiated directly rather than inside a ControlTemplate).","triggerScenarios":"Placing <AdornedElementPlaceholder/> directly in page/window XAML instead of inside a ControlTemplate; creating an AdornedElementPlaceholder instance in code (new AdornedElementPlaceholder()) outside any template.","commonSituations":"Developer tries to use the placeholder as a standalone decorative element; copying the ErrorTemplate XAML fragment out of its template context; testing the control in isolation.","solutions":["Use AdornedElementPlaceholder only inside a ControlTemplate (typically a Validation ErrorTemplate)","Set Validation.ErrorTemplate on the bound control instead of hosting the placeholder directly","If you need a standalone adornment, use Adorner/AdornerLayer APIs directly"],"exampleFix":"<!-- before -->\n<StackPanel>\n  <AdornedElementPlaceholder/>\n</StackPanel>\n<!-- after -->\n<ControlTemplate x:Key=\"errorTemplate\">\n  <AdornedElementPlaceholder Name=\"placeholder\"/>\n</ControlTemplate>\n<TextBox Validation.ErrorTemplate=\"{StaticResource errorTemplate}\" .../>","handlingStrategy":"validation","validationCode":"if (placeholder.TemplatedParent == null)\n    throw new InvalidOperationException(\"AdornedElementPlaceholder must be inside a ControlTemplate.\");\n","typeGuard":null,"tryCatchPattern":"try\n{\n    InitializePlaceholder();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"template\"))\n{\n    // restructure XAML to place the control in a template\n}\n","preventionTips":["Only use AdornedElementPlaceholder inside Validation ErrorTemplate","Never add it to a panel's Children","Prefer built-in ValidatesOnExceptions/ErrorTemplate patterns"],"tags":["wpf","invalid-operation","xaml-template"],"backgroundTag":"invalid-state-transition","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"}