{"record":{"id":"57297bd3ee80e340","repo":"dotnet/wpf","slug":"sr-elementmustbelongtotemplate","errorCode":null,"errorMessage":"SR.ElementMustBelongToTemplate","messagePattern":"SR\\.ElementMustBelongToTemplate","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DependencyPropertyHelper.cs","lineNumber":203,"sourceCode":"        /// </summary>\n        /// <param name=\"elementInTemplate\">element belonging to a template instance</param>\n        /// <param name=\"dependencyProperty\">property</param>\n        /// <remarks>\n        /// This method provides more detailed information in cases where\n        /// the BaseValueSource is ParentTemplate.  The information is primarily\n        /// of use to diagnostic tools.\n        /// </remarks>\n        public static bool IsTemplatedValueDynamic(DependencyObject elementInTemplate, DependencyProperty dependencyProperty)\n        {\n            ArgumentNullException.ThrowIfNull(elementInTemplate);\n            ArgumentNullException.ThrowIfNull(dependencyProperty);\n\n            FrameworkObject child = new FrameworkObject(elementInTemplate);\n            DependencyObject templatedParent = child.TemplatedParent;\n\n            if (templatedParent == null)\n            {\n                throw new ArgumentException(SR.ElementMustBelongToTemplate, nameof(elementInTemplate));\n            }\n\n            int templateChildIndex = child.TemplateChildIndex;\n            return StyleHelper.IsValueDynamic(templatedParent, templateChildIndex, dependencyProperty);\n        }\n    }\n}\n","sourceCodeStart":185,"sourceCodeEnd":211,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DependencyPropertyHelper.cs#L185-L211","documentation":"DependencyPropertyHelper.IsTemplatedValueDynamic determines whether a property value on an element inside a template is dynamic (resource/perf dependency). The argument must be an element that belongs to a template; if its TemplatedParent is null the element does not belong to any template, so an ArgumentException naming elementInTemplate is thrown.","triggerScenarios":"Calling DependencyPropertyHelper.IsTemplatedValueDynamic(element, property) with an element whose TemplatedParent is null — e.g. a top-level window, a control created directly (not via template expansion), or an element removed from its templated parent.","commonSituations":"Querying the root Window/Page itself instead of a visual child inside its ControlTemplate/DataTemplate, querying elements after a template was re-applied (invalidating TemplatedParent), or passing elements from a plain visual tree that never came from templating.","solutions":["Pass only elements that are inside a template: check element.TemplatedParent != null before calling","Move up/down the tree to a child that was instantiated by the template rather than the templated control itself","Re-query after template application is complete (e.g. after the element is loaded and templated), not before"],"exampleFix":"// before\nDependencyPropertyHelper.IsTemplatedValueDynamic(myWindow, Window.BackgroundProperty); // TemplatedParent is null\n// after\nif (myWindow.TemplatedParent != null)\n    DependencyPropertyHelper.IsTemplatedValueDynamic(myWindow, Window.BackgroundProperty);","handlingStrategy":"type-guard","validationCode":"if (element == null || element.TemplatedParent == null)\n    throw new ArgumentException(\"Element must belong to a template (TemplatedParent != null).\");","typeGuard":"static bool IsInTemplate(DependencyObject e) => e is FrameworkObject f ? f.TemplatedParent != null : e is FrameworkElement fe && fe.TemplatedParent != null;","tryCatchPattern":"try { return DependencyPropertyHelper.IsTemplatedValueDynamic(element, prop); }\ncatch (ArgumentException ex) when (ex.ParamName == \"elementInTemplate\") { /* treat as non-templated element */ }","preventionTips":["Check element.TemplatedParent != null before calling IsTemplatedValueDynamic","Query visual children instantiated by the template, not the templated control itself","Call after templating has been applied (element loaded), not during construction"],"tags":["wpf","templates","argument"],"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"}