{"record":{"id":"2acb395403f38476","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeframeworkderived-d-gettype","errorCode":null,"errorMessage":"SR.Format(SR.MustBeFrameworkDerived, d.GetType())","messagePattern":"SR\\.Format\\(SR\\.MustBeFrameworkDerived, d\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":2334,"sourceCode":"        // to handle Visual3D (workaround for PDC)\n\n        internal static void SpecialDowncastToFEorFCE(DependencyObject d,\n                                    out FrameworkElement fe, out FrameworkContentElement fce,\n                                    bool throwIfNeither)\n        {\n            if (d is FrameworkElement frameworkElement)\n            {\n                fe = frameworkElement;\n                fce = null;\n            }\n            else if (d is FrameworkContentElement frameworkContentElement)\n            {\n                fe = null;\n                fce = frameworkContentElement;\n            }\n            else if (throwIfNeither && !(d is System.Windows.Media.Media3D.Visual3D) )\n            {\n                throw new InvalidOperationException(SR.Format(SR.MustBeFrameworkDerived, d.GetType()));\n            }\n            else\n            {\n                fe = null;\n                fce = null;\n            }\n        }\n\n\n        #endregion ClearGeneratedSubTree\n\n        //  ===========================================================================\n        //  These methods are invoked when an Event\n        //  is routed through a tree\n        //  ===========================================================================\n\n        #region InvokeEventTriggers\n","sourceCodeStart":2316,"sourceCodeEnd":2352,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L2316-L2352","documentation":"StyleHelper's framework-element/content-element extraction helper throws when a DependencyObject is neither FrameworkElement, FrameworkContentElement, nor Visual3D and the caller required one of those (throwIfNeither). WPF styling can only be applied to these kinds of objects, so anything else is rejected.","triggerScenarios":"Calling the FindTemplateChildInStyle / templated-parent helpers with a raw DependencyObject (e.g. DependencyObject, Freezable, or a plain Visual) that is not FrameworkElement/FrameworkContentElement/Visual3D.","commonSituations":"Custom framework code walking visual trees that includes non-FE leaves (adorners' internals, raw Drawings); passing a Freezable resource where an element is expected; library code bridging to StyleHelper with the wrong node type.","solutions":["Ensure the object derives from FrameworkElement or FrameworkContentElement (or Visual3D) before invoking styling APIs","Check the object type with a guard before calling, e.g. d is FrameworkElement || d is FrameworkContentElement || d is Visual3D","If the intent was non-throwing, use the overload/path with throwIfNeither=false","Move custom logic off DependencyObject to FrameworkElement to get styling support"],"exampleFix":"// before\nStyleHelper.FindTemplateChildInStyle(..., (DependencyObject)new Freezable(), ...); // throws\n// after\nif (d is FrameworkElement fe || d is FrameworkContentElement fce || d is Visual3D v3d)\n    StyleHelper.FindTemplateChildInStyle(..., d, ...);","handlingStrategy":"type-guard","validationCode":"bool IsStyleable(DependencyObject d) =>\n    d is FrameworkElement || d is FrameworkContentElement || d is Visual3D;","typeGuard":"bool IsStyleable(DependencyObject d) => d is FrameworkElement fe || d is FrameworkContentElement fce || d is System.Windows.Media.Media3D.Visual3D v;","tryCatchPattern":"try { ResolveTemplateChild(d); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Framework\")) { Log(d.GetType().FullName + \" is not styleable\"); }","preventionTips":["Check object lineage before passing to styling helpers","Restrict custom helpers to FrameworkElement inputs","Handle Visual3D as a distinct branch in tree walkers"],"tags":["wpf","type-mismatch","frameworkelement"],"backgroundTag":"type-mismatch","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"}