{"record":{"id":"f23f8c1299f28b41","repo":"dotnet/wpf","slug":"sr-format-sr-modifyinglogicaltreeviastylesnotimplemented","errorCode":null,"errorMessage":"SR.Format(SR.ModifyingLogicalTreeViaStylesNotImplemented, value, \"FrameworkElementFactory.SetValue\")","messagePattern":"SR\\.Format\\(SR\\.ModifyingLogicalTreeViaStylesNotImplemented, value, \"FrameworkElementFactory\\.SetValue\"\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":215,"sourceCode":"            if (_sealed)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"FrameworkElementFactory\"));\n            }\n\n            ArgumentNullException.ThrowIfNull(dp);\n\n            // Value needs to be valid for the DP, or Binding/MultiBinding/PriorityBinding.\n            //  (They all have MarkupExtension, which we don't actually support, see above check.)\n\n            if (!dp.IsValidValue(value) && !(value is MarkupExtension) && !(value is DeferredReference))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidPropertyValue, value, dp.Name));\n            }\n\n            // Styling the logical tree is not supported\n            if (StyleHelper.IsStylingLogicalTree(dp, value))\n            {\n                throw new NotSupportedException(SR.Format(SR.ModifyingLogicalTreeViaStylesNotImplemented, value, \"FrameworkElementFactory.SetValue\"));\n            }\n\n            if (dp.ReadOnly)\n            {\n                // Read-only properties will not be consulting FrameworkElementFactory for value.\n                //  Rather than silently do nothing, throw error.\n                throw new ArgumentException(SR.Format(SR.ReadOnlyPropertyNotAllowed, dp.Name, GetType().Name));\n            }\n\n            ResourceReferenceExpression resourceExpression = value as ResourceReferenceExpression;\n            DynamicResourceExtension dynamicResourceExtension = value as DynamicResourceExtension;\n            object resourceKey = null;\n\n            if( resourceExpression != null )\n            {\n                resourceKey = resourceExpression.ResourceKey;\n            }\n            else if( dynamicResourceExtension != null )","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L197-L233","documentation":"FrameworkElementFactory.SetValue throws NotSupportedException when the value would modify (style) the logical tree. Values such as elements or logical-tree-touching references are not allowed to be set through a style factory because styles must not reparent or modify the logical tree. WPF deliberately disallows this scenario rather than producing undefined behavior at instantiation.","triggerScenarios":"Calling factory.SetValue(dp, value) where StyleHelper.IsStylingLogicalTree(dp, value) returns true — typically setting a property to a UIElement or a value that would be added to the logical tree via the style.","commonSituations":"Trying to set a Content or Child property to an element instance on a FrameworkElementFactory; attempting to embed child visuals via factory SetValue instead of AddChild; code translated from templates that worked with direct element construction.","solutions":["Use factory.AppendChild(childFactory) with a nested FrameworkElementFactory instead of SetValue with an element.","Set logical-tree properties after template instantiation on the actual element instance.","If a plain value is intended, verify you are passing a value object, not a UIElement, to SetValue."],"exampleFix":"// before\nfactory.SetValue(ContentPresenter.ContentProperty, new TextBlock());\n// after\nvar textFactory = new FrameworkElementFactory(typeof(TextBlock));\nfactory.AppendChild(textFactory);","handlingStrategy":"validation","validationCode":"if (value is UIElement) throw new InvalidOperationException(\"Use AppendChild, not SetValue, for logical-tree values.\");","typeGuard":"static bool IsLogicalTreeValue(object value) => value is UIElement or ContentElement;","tryCatchPattern":"try { factory.SetValue(dp, value); }\ncatch (NotSupportedException) { var child = new FrameworkElementFactory(value.GetType()); factory.AppendChild(child); }","preventionTips":["Never pass UIElement instances to factory SetValue.","Model child content with nested FrameworkElementFactory + AppendChild.","Set element-instance content after template instantiation."],"tags":["wpf","logical-tree","styles"],"backgroundTag":"unsupported-operation","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"}