{"record":{"id":"a7f6c50159cd1786","repo":"dotnet/wpf","slug":"sr-format-sr-tableinvalidparentnodetype-newparent-gettype","errorCode":null,"errorMessage":"SR.Format(SR.TableInvalidParentNodeType, newParent.GetType().ToString())","messagePattern":"SR\\.Format\\(SR\\.TableInvalidParentNodeType, newParent\\.GetType\\(\\)\\.ToString\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TableCell.cs","lineNumber":88,"sourceCode":"        //\n        //------------------------------------------------------\n\n        #region Public Methods \n\n        /// <summary>\n        /// Called when tablecell gets new parent\n        /// </summary>\n        /// <param name=\"newParent\">\n        /// New parent of cell\n        /// </param>\n        internal override void OnNewParent(DependencyObject newParent)\n        {\n            DependencyObject oldParent = this.Parent;\n            TableRow newParentTR = newParent as TableRow;\n\n            if((newParent != null) && (newParentTR == null))\n            {\n                throw new InvalidOperationException(SR.Format(SR.TableInvalidParentNodeType, newParent.GetType().ToString()));\n            }\n\n            if(oldParent != null)\n            {\n                ((TableRow)oldParent).Cells.InternalRemove(this);\n            } \n\n            base.OnNewParent(newParent);\n\n            if ((newParentTR != null) && (newParentTR.Cells != null)) // keep PreSharp happy\n            {\n                newParentTR.Cells.InternalAdd(this);\n            }\n        }\n\n        #endregion Public Methods \n\n        //------------------------------------------------------","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TableCell.cs#L70-L106","documentation":"TableCell.OnNewParent validates that a cell's new parent is a TableRow (or null). Assigning a TableCell to any other DependencyObject parent throws InvalidOperationException naming the actual parent type, because TableCell can only live in a TableRow's Cells collection.","triggerScenarios":"Programmatically setting a TableCell's parent (via collection add into the wrong collection or direct reparenting APIs) to something that is not a TableRow, e.g. adding a cell to a TableRowGroup or Table directly.","commonSituations":"Building table markup programmatically and appending cells to the wrong container; XAML with a TableCell directly under TableRowGroup/Table; data-driven table generation with wrong nesting.","solutions":["Add the TableCell to a TableRow's Cells collection, not to TableRowGroup/Table","Correct the XAML nesting: Table > TableRowGroup > TableRow > TableCell","When reparenting, ensure newParent is TableRow or null before calling"],"exampleFix":"// before\ntableRowGroup.Cells.Add(cell); // wrong container\n// after\nvar row = new TableRow();\nrow.Cells.Add(cell);\ntableRowGroup.Rows.Add(row);","handlingStrategy":"validation","validationCode":"bool ok = newParent == null || newParent is TableRow;","typeGuard":"static bool CanParentCell(DependencyObject p) => p is TableRow || p == null;","tryCatchPattern":"try { row.Cells.Add(cell); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"parent\")) { /* fix nesting */ }","preventionTips":["Follow Table > TableRowGroup > TableRow > TableCell nesting","Add cells only via TableRow.Cells","Validate XAML structure for programmatic table builds"],"tags":["wpf","table","xaml-nesting"],"backgroundTag":"invalid-parent-element","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"}