{"record":{"id":"21d8c76c28ef59de","repo":"dotnet/wpf","slug":"sr-visual-nocommonancestor","errorCode":null,"errorMessage":"SR.Visual_NoCommonAncestor","messagePattern":"SR\\.Visual_NoCommonAncestor","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Visual.cs","lineNumber":4364,"sourceCode":"            return descendant.InternalTransformToAncestor(this, true);\n        }\n\n\n        /// <summary>\n        /// The returned matrix can be used to transform coordinates from this Visual to\n        /// the specified Visual.\n        /// Returns null if no such transform exists due to a non-invertible Transform.\n        /// </summary>\n        /// <exception cref=\"ArgumentNullException\">If visual is null.</exception>\n        /// <exception cref=\"InvalidOperationException\">If the Visuals are not connected.</exception>\n        public GeneralTransform TransformToVisual(Visual visual)\n        {\n            DependencyObject ancestor = FindCommonVisualAncestor(visual);\n            Visual ancestorAsVisual = ancestor as Visual;\n\n            if (ancestorAsVisual == null)\n            {\n                throw new System.InvalidOperationException(SR.Visual_NoCommonAncestor);\n            }\n\n            GeneralTransform g0;\n            Matrix m0;\n\n            bool isSimple0 = this.TrySimpleTransformToAncestor(ancestorAsVisual,\n                                                               false,\n                                                               out g0,\n                                                               out m0);\n\n            GeneralTransform g1;\n            Matrix m1;\n\n            bool isSimple1 = visual.TrySimpleTransformToAncestor(ancestorAsVisual,\n                                                                 true,\n                                                                 out g1,\n                                                                 out m1);\n","sourceCodeStart":4346,"sourceCodeEnd":4382,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Visual.cs#L4346-L4382","documentation":"Visual.TransformToDescendant (and related transform APIs) throws InvalidOperationException(SR.Visual_NoCommonAncestor) when the two visuals share no common visual ancestor, so no coordinate transform between them can be computed.","triggerScenarios":"Calling TransformToDescendant(visual) where the target visual is in a different visual tree / has no common ancestor with this visual.","commonSituations":"Transforming between elements from different windows, popups, or an element not yet connected to the visual tree (e.g. before it is loaded).","solutions":["Ensure both visuals belong to the same visual tree (same window/popup) before transforming","Wait until the target element is loaded/connected (handle Loaded event)","Check FindCommonVisualAncestor logic: verify both are attached via Window.GetWindow / PresentationSource"],"exampleFix":"// before\nvar t = otherWindowElement.TransformToDescendant(this); // different window\n// after\nif (Window.GetWindow(otherWindowElement) == Window.GetWindow(this))\n    var t = otherWindowElement.TransformToDescendant(this);","handlingStrategy":"validation","validationCode":"bool SameTree(Visual a, Visual b) => Window.GetWindow(a) == Window.GetWindow(b) && PresentationSource.FromVisual(a) != null && PresentationSource.FromVisual(b) != null;","typeGuard":"bool CanTransform(Visual a, Visual b) => SameTree(a, b);","tryCatchPattern":"try { var t = source.TransformToDescendant(target); } catch (InvalidOperationException) { /* elements not in same tree */ }","preventionTips":["Only transform between visuals in the same window/popup","Wait for Loaded before transforms","Re-check relationships after re-parenting"],"tags":["wpf","visual-tree","transform","no-common-ancestor"],"backgroundTag":"unsupported-operation","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"}