{"record":{"id":"b7a9891ce5895cb7","repo":"AvaloniaUI/Avalonia","slug":"cannot-determine-visual-parent","errorCode":null,"errorMessage":"Cannot determine visual parent.","messagePattern":"Cannot determine visual parent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"samples/ControlCatalog/ViewModels/TransitioningContentControlPageViewModel.cs","lineNumber":314,"sourceCode":"        /// <param name=\"to\">The to control.</param>\n        /// <returns>The common parent.</returns>\n        /// <exception cref=\"ArgumentException\">\n        /// The two controls do not share a common parent.\n        /// </exception>\n        /// <remarks>\n        /// Any one of the parameters may be null, but not both.\n        /// </remarks>\n        private static Visual GetVisualParent(Visual? from, Visual? to)\n        {\n            var p1 = (from ?? to)!.GetVisualParent();\n            var p2 = (to ?? from)!.GetVisualParent();\n\n            if (p1 != null && p2 != null && p1 != p2)\n            {\n                throw new ArgumentException(\"Controls for PageSlide must have same parent.\");\n            }\n\n            return p1 ?? throw new InvalidOperationException(\"Cannot determine visual parent.\");\n        }\n    }\n}\n","sourceCodeStart":296,"sourceCodeEnd":318,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/samples/ControlCatalog/ViewModels/TransitioningContentControlPageViewModel.cs#L296-L318","documentation":"Thrown by TransitioningContentControlPageViewModel.GetVisualParent (InvalidOperationException) when neither control resolves a visual parent (p1 is null). The transition cannot proceed because there is no parent container to animate within.","triggerScenarios":"Calling GetVisualParent(from, to) where (from ?? to).GetVisualParent() returns null — the single available control is not attached to the visual tree, so it has no parent.","commonSituations":"Invoking a transition on content that has not yet been added to the visual tree; controls that were detached/removed before the transition completed; transitioning during initial load before layout attaches the element.","solutions":["Ensure the control is attached to the visual tree (has a parent) before starting the transition.","Defer the transition until the Loaded event confirms the control is in the tree.","Provide at least one control with a valid parent so p1 is non-null."],"exampleFix":"// before\nvar parent = GetVisualParent(from, to); // control detached -> p1 null -> throws\n\n// after\nif ((from ?? to)?.GetVisualParent() == null) { await control.WaitForLoaded(); }\nvar parent = GetVisualParent(from, to);","handlingStrategy":"validation","validationCode":"if ((from ?? to)?.GetVisualParent() == null) { /* wait for Loaded before transitioning */ }","typeGuard":"static bool IsAttachedToTree(Visual? v) => v?.GetVisualParent() != null;","tryCatchPattern":"try { var p = GetVisualParent(from, to); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"visual parent\")) { /* await Loaded then retry */ }","preventionTips":["Start transitions only after the control's Loaded event.","Ensure at least one control is attached to the visual tree.","Cancel transitions for detached content."],"tags":["sample","transition","visual-tree","csharp","invalidoperation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}