{"record":{"id":"352ca4e8baba69ff","repo":"unoplatform/uno","slug":"unable-to-find-x-datatype-in-enclosing-datatemplat","errorCode":null,"errorMessage":"Unable to find x:DataType in enclosing DataTemplate for x:Bind event","messagePattern":"Unable to find x:DataType in enclosing DataTemplate for x:Bind event","errorType":"exception","errorClass":"XamlGenerationException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs","lineNumber":3833,"sourceCode":"\t\t\t\tINamedTypeSymbol GetTargetType()\n\t\t\t\t{\n\t\t\t\t\tif (template.isInside)\n\t\t\t\t\t{\n\t\t\t\t\t\tvar dataTypeObject = FindMember(template.xamlObject!, \"DataType\", XamlConstants.XamlXmlNamespace);\n\t\t\t\t\t\tif (dataTypeObject?.Value == null)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// Check if this is a static method binding (path starts with a namespace prefix like \"local:Type\")\n\t\t\t\t\t\t\t// Static bindings don't require x:DataType because they don't access the DataContext\n\t\t\t\t\t\t\tvar pathParts = path.Split('.');\n\t\t\t\t\t\t\tvar isStaticBinding = pathParts.FirstOrDefault()?.Contains(\":\") ?? false;\n\n\t\t\t\t\t\t\tif (isStaticBinding)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t// Extract the type from the static path (e.g., \"local:StaticHandler\" from \"local:StaticHandler.OnClick\")\n\t\t\t\t\t\t\t\treturn GetType(RewriteNamespaces(pathParts[0]));\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tthrow new XamlGenerationException(\"Unable to find x:DataType in enclosing DataTemplate for x:Bind event\", bind);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn GetType(dataTypeObject.Value.ToString() ?? \"\");\n\t\t\t\t\t}\n\t\t\t\t\telse if (_xClassName?.Symbol is not null)\n\t\t\t\t\t{\n\t\t\t\t\t\treturn _xClassName.Symbol;\n\t\t\t\t\t}\n\t\t\t\t\telse if (Generation.AutoGeneratedCodeBehindFiles.TryGetValue(_fileDefinition.UniqueID, out var autoGenBaseType))\n\t\t\t\t\t{\n\t\t\t\t\t\t// Use the XAML-derived base type when code-behind is auto-generated\n\t\t\t\t\t\treturn autoGenBaseType;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tthrow new XamlGenerationException($\"Unable to find the type '{_xClassName?.Namespace}.{_xClassName?.ClassName}'\", bind);\n\t\t\t\t\t}\n\t\t\t\t}","sourceCodeStart":3815,"sourceCodeEnd":3851,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs#L3815-L3851","documentation":"Thrown in the x:Bind event GetTargetType() logic when the binding is inside a DataTemplate (template.isInside) but the DataTemplate has no x:DataType attribute, and the path is not a static binding (does not start with a namespace prefix like 'local:'). x:Bind inside a DataTemplate is compiled and strongly typed, so it requires x:DataType to know the data type of each item; without it (and not being static) the generator cannot resolve the handler.","triggerScenarios":"Using {x:Bind SomeMethod} on an event inside a <DataTemplate> that has no x:DataType; a DataTemplate used for an ItemsControl where x:DataType was omitted.","commonSituations":"Adding x:Bind to a templated item without setting x:DataType; porting from {Binding} (late-bound) to {x:Bind} (compiled) and forgetting the type annotation; assuming DataContext propagates into templates for x:Bind (it does not — x:Bind uses x:DataType).","solutions":["Add x:DataType=\"local:MyItemViewModel\" to the DataTemplate, matching the item type.","If the handler is a static method, qualify the path with a namespace prefix, e.g. Click=\"{x:Bind local:Handlers.OnClick}\" — this bypasses the x:DataType requirement.","Move the handler to the page/code-behind and reference the page-level type instead of a template-scoped binding."],"exampleFix":"<!-- before -->\n<DataTemplate>\n  <Button Click=\"{x:Bind Delete}\"/>\n</DataTemplate>\n\n<!-- after -->\n<DataTemplate x:DataType=\"local:ItemViewModel\">\n  <Button Click=\"{x:Bind Delete}\"/>\n</DataTemplate>","handlingStrategy":"validation","validationCode":"# Flag DataTemplate containing {x:Bind} without x:DataType (and not static)\nGet-ChildItem -Recurse -Filter *.xaml | ForEach-Object {\n  $text = Get-Content $_.FullName -Raw\n  [regex]::Matches($text, '<DataTemplate(?![^>]*x:DataType)(.*?)</DataTemplate>', 'Singleline') |\n    ForEach-Object {\n      if ($_.Groups[1].Value -match '\\{x:Bind(?!.*:)') {\n        Write-Warning \"DataTemplate with x:Bind but no x:DataType (and non-static path)\"\n      }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set x:DataType on any DataTemplate that uses {x:Bind}.","For static handlers, qualify the path with a namespace prefix (local:Type.Method) to bypass x:DataType.","Remember x:Bind is compiled/strongly-typed and does not inherit DataContext like {Binding}."],"tags":["xaml","x-bind","data-template","x-datatype","build"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}