{"record":{"id":"a0e00da9cdac4d39","repo":"dotnet/wpf","slug":"sr-fixeddocumentexpectsdependencyobject","errorCode":null,"errorMessage":"SR.FixedDocumentExpectsDependencyObject","messagePattern":"SR\\.FixedDocumentExpectsDependencyObject","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs","lineNumber":444,"sourceCode":"                    asyncRequest.Cancelled = true;\n                    asyncRequest.PageContent.GetPageRootAsyncCancel();\n                }\n            }\n        }\n\n        /// <summary>\n        /// <see cref=\"DynamicDocumentPaginator.GetObjectPosition\"/>\n        /// </summary>\n        /// <exception cref=\"ArgumentNullException\">element is NULL.</exception>\n        internal ContentPosition GetObjectPosition(Object o)\n        {\n            ArgumentNullException.ThrowIfNull(o);\n\n            DependencyObject element = o as DependencyObject;\n\n            if (element == null)\n            {\n                throw new ArgumentException(SR.FixedDocumentExpectsDependencyObject);\n            }\n            DocumentsTrace.FixedFormat.IDF.Trace($\"IDF.GetContentPositionForElement({element})\");\n            // Make sure that the call is in the right context.\n//             Dispatcher.VerifyAccess();\n\n\n            // walk up the logical parent chain to find the containing page\n            FixedPage fixedPage = null;\n            int pageIndex = -1;\n            if (element != this)\n            {\n                DependencyObject el = element;\n                while (el != null)\n                {\n                    fixedPage = el as FixedPage;\n\n                    if (fixedPage != null)\n                    {","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs#L426-L462","documentation":"FixedDocument.GetObjectPosition throws ArgumentException with SR.FixedDocumentExpectsDependencyObject when the object passed to it is not a DependencyObject. Because WPF document content elements (PageContent, FixedPage, etc.) are DependencyObjects, the API refuses any plain CLR object that cannot participate in the dependency property system.","triggerScenarios":"Calling GetObjectPosition (the inverse of GetContentPositionForElement) with a POCO, a string, or a non-WPF object instead of a document element such as PageContent or FixedPage.","commonSituations":"Passing view-model or data items instead of the actual visual/document elements; passing content loaded from a non-WPF pipeline; null-checking was fine but the object's type was wrong.","solutions":["Pass the actual DependencyObject document element (e.g. PageContent containing the FixedPage) rather than the underlying data object.","Guard before calling: if (!(o is DependencyObject)) resolve the corresponding element first.","If you hold a data item, map it to its content element (e.g. via ItemsControl.ItemContainerGenerator-style lookup) before querying the position."],"exampleFix":"// before\nobject position = fixedDocument.GetObjectPosition(myDataItem);\n\n// after\nif (myDataItem is DependencyObject element)\n{\n    object position = fixedDocument.GetObjectPosition(element);\n}","handlingStrategy":"type-guard","validationCode":"bool isDependencyObject = o is System.Windows.DependencyObject;","typeGuard":"static bool IsDependencyObject(object o) => o is System.Windows.DependencyObject;","tryCatchPattern":"try\n{\n    var position = fixedDocument.GetObjectPosition(element);\n}\ncatch (ArgumentException ex)\n{\n    logger.LogWarning(ex, \"GetObjectPosition requires a DependencyObject; got {Type}.\", element?.GetType().Name);\n}","preventionTips":["Pass WPF document elements (PageContent/FixedPage), not view-model or data objects.","Map data items to their corresponding content elements before calling position APIs.","Enable analyzer/runtime checks that catch non-DependencyObject usage early in document code."],"tags":["wpf","invalid-argument","dependencyobject","type-mismatch"],"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"}