{"record":{"id":"5725546c689df69a","repo":"dotnet/wpf","slug":"sr-format-sr-namedobjectmustbeframeworkelement-targetname","errorCode":null,"errorMessage":"SR.Format(SR.NamedObjectMustBeFrameworkElement, targetName)","messagePattern":"SR\\.Format\\(SR\\.NamedObjectMustBeFrameworkElement, targetName\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs","lineNumber":487,"sourceCode":"            else\n            {\n                DependencyObject targetObject = null;\n\n                targetObject = LogicalTreeHelper.FindLogicalNode( startElement, targetName );\n\n                if( targetObject == null )\n                {\n                    throw new ArgumentException( SR.Format(SR.TargetNameNotFound, targetName));\n                }\n\n                FrameworkObject fo = new FrameworkObject(targetObject);\n                if( fo.IsFE )\n                {\n                    targetFE = fo.FE;\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Format(SR.NamedObjectMustBeFrameworkElement, targetName));\n                }\n            }\n\n            return targetFE;\n        }\n\n        /// <summary>\n        ///     Triggers associated with this object.  Both the triggering condition\n        /// and the trigger effect may be on this object or on its tree child\n        /// objects.\n        /// </summary>\n        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]\n        public TriggerCollection Triggers\n        {\n            get\n            {\n                TriggerCollection triggerCollection = EventTrigger.TriggerCollectionField.GetValue(this);\n                if (triggerCollection == null)","sourceCodeStart":469,"sourceCodeEnd":505,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs#L469-L505","documentation":"FindNamedFrameworkElement resolves a registered name (x:Name/Name) to a FrameworkElement during template or name-scope lookup. If the object registered under targetName exists but is not a FrameworkElement (e.g. a Freezable or other DependencyObject), the lookup cannot return it and throws InvalidOperationException. WPF requires named template targets used as element targets to be actual FrameworkElements.","triggerScenarios":"Calling FindName-related template APIs (e.g. FrameworkTemplate.FindName, TemplateContent lookup) with a targetName that resolves to a non-FrameworkElement DependencyObject in the name scope.","commonSituations":"x:Name placed on a non-element resource like a Brush, Geometry, or storyboard child inside a template; referencing a named Freezable from TemplateBinding/templated-parent code; name registered by parser on a value-type resource.","solutions":["Rename the offending resource so the targetName points at an actual FrameworkElement (Control, Panel, etc.)","If you need the non-element object, look it up via FindResource/TryFindResource instead of name-scope element lookup","Move the named resource out of the template's name scope or use x:Key with a ResourceDictionary instead of x:Name","Cast defensively: var fe = FindName(n) as FrameworkElement; if (fe == null) handle non-element case"],"exampleFix":"// before\n<LinearGradientBrush x:Name=\"bg\" .../>\nvar target = (FrameworkElement)template.FindName(\"bg\", templatedParent);\n// after\n<Border x:Name=\"bg\" Background=\"...\"/>\nvar target = (FrameworkElement)template.FindName(\"bg\", templatedParent);","handlingStrategy":"validation","validationCode":"object named = nameScope.FindName(targetName);\nif (named is not FrameworkElement) throw new InvalidOperationException($\"{targetName} must be a FrameworkElement\");","typeGuard":"bool IsNamedFrameworkElement(object o) => o is FrameworkElement;","tryCatchPattern":"try { var fe = FindNamedFrameworkElement(nameScope, targetName); }\ncatch (InvalidOperationException) { /* name resolves to non-element; fall back to FindResource */ }","preventionTips":["Use x:Key on resources, x:Name only on elements","Never x:Name Freezables referenced as template targets","Prefer TryFindResource for non-element lookups"],"tags":["wpf","name-scope","invalid-operation"],"backgroundTag":"type-mismatch","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"}