{"record":{"id":"35b0a720cfb8b219","repo":"unoplatform/uno","slug":"cannot-assign-child-of-type-childtype-to-prope","errorCode":null,"errorMessage":"Cannot assign child of type '{childType}' to property of type '{propertyType.GetFullyQualifiedTypeExcludingGlobal()}'","messagePattern":"Cannot assign child of type '(.+?)' to property of type '(.+?)'","errorType":"exception","errorClass":"XamlGenerationException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs","lineNumber":3457,"sourceCode":"\t\t\t\t\t\t\t\t\t\t// Plain object\n\t\t\t\t\t\t\t\t\t\twriter.AppendLineIndented(\n\t\t\t\t\t\t\t\t\t\t\t$\"var {localCollectionName} = {writer.AppliedParameterName}.{member.Member.Name};\"\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\tforeach (var inner in member.Objects)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\twriter.AppendIndented($\"{localCollectionName}.Add(\");\n\n\t\t\t\t\t\t\t\t\t\tBuildChild(writer, member, inner);\n\n\t\t\t\t\t\t\t\t\t\twriter.AppendLineIndented(\");\");\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse if (member.Objects.Count == 1)\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tvar childType = GetType(member.Objects[0].Type).GetFullyQualifiedTypeExcludingGlobal();\n\t\t\t\t\t\t\t\t\tthrow new XamlGenerationException($\"Cannot assign child of type '{childType}' to property of type '{propertyType.GetFullyQualifiedTypeExcludingGlobal()}'\", member.Objects[0]);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tthrow new XamlGenerationException($\"The property '{member.Member.Name}' of type '{propertyType}' does not support adding multiple objects\", member);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t// GenerateWarning(writer, $\"Unknown type {objectDefinition.Type} for property {member.Member.DeclaringType}\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvar isMemberInsideResourceDictionary = IsMemberInsideResourceDictionary(objectDefinition);\n\t\t\t\t\t\t\tvar value = member.Value?.ToString();\n\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tmember.Member.Name == \"Name\"","sourceCodeStart":3439,"sourceCodeEnd":3475,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs#L3439-L3475","documentation":"Thrown in the property-member handling when a property expects exactly one child object (member.Objects.Count == 1) but the child's resolved type is not assignable to the property's type. The generator emits the child type via GetType and the property type from the member symbol and finds them incompatible, so it refuses to emit an invalid assignment.","triggerScenarios":"Assigning an element of type A to a property typed as unrelated type B, e.g. <Grid.RowDefinitions><RowDefinition/></Grid.RowDefinitions> is fine but <SomeControl.Header><RowDefinition/></SomeControl.Header> is not; nested an object whose type does not match the collection/item type the property expects when there is exactly one object.","commonSituations":"Wrong element inside a property-element tag; a type that used to be assignable but changed after an update; misunderstanding which type a property accepts (e.g. putting a UIElement where a DataTemplate is required).","solutions":["Check the property's documented type and place an element whose type matches (or is assignable to) it.","Wrap the content in the expected type (e.g. wrap in <DataTemplate> if a template is required).","If the property is a collection, ensure the child type implements the collection's element type.","Verify no namespace collision is resolving the element to an unexpected type."],"exampleFix":"<!-- before -->\n<ContentControl>\n  <ContentControl.Content>\n    <RowDefinition/>  <!-- wrong type -->\n  </ContentControl.Content>\n</ContentControl>\n\n<!-- after -->\n<ContentControl>\n  <ContentControl.Content>\n    <TextBlock Text=\"Hello\"/>\n  </ContentControl.Content>\n</ContentControl>","handlingStrategy":"type-guard","validationCode":"# Confirm the element type is assignable to the property type by checking the docs/API\n# Pre-build: review each property-element tag and verify the child type matches the property's declared type.\n# In code-behind, the equivalent guard is a type check before assignment:\nif (child is TargetType typed) { control.Property = typed; }","typeGuard":"// C# type guard for programmatic construction (mirrors what XAML enforces):\nstatic void SetContent(object control, object child)\n{\n    if (control is ContentControl cc && child is UIElement uie)\n    {\n        cc.Content = uie;\n    }\n    else\n    {\n        throw new ArgumentException($\"Cannot assign {child?.GetType()} to content\");\n    }\n}","tryCatchPattern":null,"preventionTips":["Look up the property's declared type in the WinUI/Uno API docs before nesting an element.","Use property-element syntax (<Control.Property>) explicitly so the assignment target is obvious.","Watch for type renames after framework version bumps."],"tags":["xaml","type-mismatch","property-assignment","build"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}