{"record":{"id":"205b1e0812ba2518","repo":"dotnet/wpf","slug":"sr-format-sr-textelementcollection-textelementtypeexpected","errorCode":null,"errorMessage":"SR.Format(SR.TextElementCollection_TextElementTypeExpected, typeof(TextElementType).Name)","messagePattern":"SR\\.Format\\(SR\\.TextElementCollection_TextElementTypeExpected, typeof\\(TextElementType\\)\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs","lineNumber":398,"sourceCode":"\n        //...................................................................\n        //\n        //  IList Members\n        //\n        //...................................................................\n\n        #region IList Members\n\n        /// <summary>\n        /// Method that does the work for IList.Add.\n        /// </summary>\n        internal virtual int OnAdd(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is TextElementType))\n            {\n                throw new ArgumentException(SR.Format(SR.TextElementCollection_TextElementTypeExpected, typeof(TextElementType).Name), nameof(value));\n            }\n\n            ValidateChild((TextElementType)value);\n\n            this.TextContainer.BeginChange();\n            try\n            {\n                bool isCacheSafePreviousIndex = _indexCache.IsValid(this);\n\n                this.Add((TextElementType)value);\n\n                return IndexOfInternal(value, isCacheSafePreviousIndex);\n            }\n            finally\n            {\n                this.TextContainer.EndChange();\n            }\n        }","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs#L380-L416","documentation":"Thrown by the internal virtual OnAdd (reached via Add / IList.Add) when the value being added is not an instance of the collection's TextElementType. Non-generic IList.Add accepts object, so the runtime type is validated here.","triggerScenarios":"Calling Add(value) or IList.Add on the collection with an object not assignable to TextElementType (e.g. adding a string or a Run to a ListItemCollection).","commonSituations":"Non-generic collection APIs used via IList; data binding or reflection feeding arbitrary objects; wrong collection/element pairing in code-built FlowDocuments.","solutions":["Add only objects of the collection's element type (e.g. Paragraph for BlockCollection, Run for InlineCollection).","Create a compatible element wrapping the content instead of adding the raw object.","Compile-time: prefer the strongly typed Add overload / collection<T> API rather than IList.Add."],"exampleFix":"// before\nIList blocks = flowDocument.Blocks;\nblocks.Add(\"hello\"); // string, not a Block\n// after\nblocks.Add(new Paragraph(new Run(\"hello\")));","handlingStrategy":"type-guard","validationCode":"if (value is TextElementType te)\n    collection.Add(te);","typeGuard":"static bool IsElementType(object value) => value is TextElementType;","tryCatchPattern":"try { ((IList)collection).Add(value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"expected\")) { /* construct a compatible TextElement and retry */ }","preventionTips":["Use the strongly typed collection API instead of IList.Add.","Know each collection's element type (BlockCollection->Block, InlineCollection->Inline, ListItemCollection->ListItem).","Wrap plain content (strings, controls) in the proper TextElement before adding."],"tags":["wpf","type-mismatch","collection"],"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"}