{"record":{"id":"7b3fda1554031ffe","repo":"dotnet/wpf","slug":"sr-format-sr-notambienttype-xamltype-name","errorCode":null,"errorMessage":"SR.Format(SR.NotAmbientType, xamlType.Name)","messagePattern":"SR\\.Format\\(SR\\.NotAmbientType, xamlType\\.Name\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs","lineNumber":432,"sourceCode":"                        break;\n                    }\n                }\n\n                lowerFrame = frame;\n                frame = (ObjectWriterFrame)frame.Previous;\n                Debug.Assert(frame is not null);\n            }\n\n            return retList;\n        }\n\n        private List<object> FindAmbientValues(XamlType[] types, bool stopAfterFirst)\n        {\n            foreach (XamlType xamlType in types)\n            {\n                if (!xamlType.IsAmbient)\n                {\n                    throw new ArgumentException(SR.Format(SR.NotAmbientType, xamlType.Name), nameof(types));\n                }\n            }\n\n            List<object> retList = new List<object>();\n\n            // Start the search for ambient properties with the parent frame.\n            ObjectWriterFrame frame = _stack.PreviousFrame;\n\n            while (frame.Depth >= 1)\n            {\n                foreach (XamlType type in types)\n                {\n                    object inst = frame.Instance;\n\n                    if (frame.XamlType is not null && frame.XamlType.CanAssignTo(type))\n                    {\n                        if (inst is not null)\n                        {","sourceCodeStart":414,"sourceCodeEnd":450,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs#L414-L450","documentation":"The FindAmbientValues(XamlType[], bool) overload validates that each XamlType in the types array has IsAmbient set. Supplying a non-ambient type throws ArgumentException with SR.NotAmbientType and paramName \"types\".","triggerScenarios":"Requesting ambient values of given ceiling types (IAmbientProvider.GetAllAmbientValues or internal valueList path) while passing XamlTypes that the schema context does not classify as ambient.","commonSituations":"Custom markup-extension or schema-context code passing ordinary types as ceiling types; the type lacks [Ambient] on its ambient properties so IsAmbient is false; using a different schema context than the one that defines the type as ambient.","solutions":["Only pass types where xamlType.IsAmbient is true — filter the array before the call.","Mark the type's ambient members with [Ambient] so the schema context reports the type as ambient.","Use the same XamlSchemaContext that produced the XamlTypes; ambient classification is context-dependent."],"exampleFix":"// before\nvar types = new[] { xt1, xt2 }; // xt2.IsAmbient == false\nFindAmbientValues(types, false);\n// after\nvar types = new[] { xt1, xt2 }.Where(t => t.IsAmbient).ToArray();\nFindAmbientValues(types, false);","handlingStrategy":"validation","validationCode":"if (types.Any(t => !t.IsAmbient))\n    throw new ArgumentException(\"All ceiling types must be ambient\", nameof(types));","typeGuard":"static bool AllAmbientTypes(IEnumerable<XamlType> types) => types.All(t => t.IsAmbient);","tryCatchPattern":"try { FindAmbientValues(types, stopAfterFirst); }\ncatch (ArgumentException ex) when (ex.ParamName == \"types\")\n{ /* identify non-ambient type from message and remove it */ }","preventionTips":["Only pass types whose schema context reports IsAmbient == true.","Mark a type as ambient via [Ambient] on its relevant properties.","Use a single XamlSchemaContext consistently so ambient metadata matches."],"tags":["wpf","xaml","ambient-properties","argument-validation"],"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-22T01:17:13.364Z"}